Java String Length Get lengthMinusTrailingWhitespace(String line)

Here you can find the source of lengthMinusTrailingWhitespace(String line)

Description

Returns the length of a string ignoring all trailing whitespace.

License

Open Source License

Parameter

Parameter Description
line the string to process

Return

the length of the string ignoring all trailing whitespace

Declaration

public static int lengthMinusTrailingWhitespace(String line) 

Method Source Code

//package com.java2s;
// License as published by the Free Software Foundation; either

public class Main {
    /**//from w w  w .  j a v  a  2  s.co m
     * Returns the length of a string ignoring all trailing whitespace. It is a
     * pity that there is not a trim() like method that only removed the
     * trailing whitespace.
     * @param line the string to process
     * @return the length of the string ignoring all trailing whitespace
     **/
    public static int lengthMinusTrailingWhitespace(String line) {
        int len = line.length();
        for (int i = len - 1; i >= 0; i--) {
            if (!Character.isWhitespace(line.charAt(i))) {
                break;
            }
            len--;
        }
        return len;
    }
}

Related

  1. lengthField(String propertyName)
  2. lengthFormat(String str, int maxLength)
  3. lengthIfAllAlpha(String str)
  4. lengthIntegers(String str)
  5. lengthMinusColors(String thisStr)
  6. lengthOfCommonPrefix(String s1, String s2)
  7. lengthOfStartingWhitespace(String s)
  8. lengthOfString(final String cadena)
  9. lengthTokensField(String fieldName)