get Count Trailing Whitespace in StringBuffer - Java java.lang

Java examples for java.lang:StringBuffer

Description

get Count Trailing Whitespace in StringBuffer

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        StringBuffer lineBuffer = new StringBuffer();
        System.out.println(getCountTrailingWhitespace(lineBuffer));
    }/*from  w  ww . j  a v a  2  s.  c o m*/

    public static int getCountTrailingWhitespace(
            final StringBuffer lineBuffer) {
        int count = 0;
        if (lineBuffer != null) {
            int length = lineBuffer.length();
            if (length > 0) {
                int index = (length - 1);
                char c = lineBuffer.charAt(index);
                while (index >= 0 && Character.isWhitespace(c)) {
                    count++;
                    index--;
                    if (index >= 0) {
                        c = lineBuffer.charAt(index);
                    }
                }
            }
        }
        return count;
    }
}

Related Tutorials