remove All Previous Space from StringBuilder - Java java.lang

Java examples for java.lang:String Whitespace

Description

remove All Previous Space from StringBuilder

Demo Code


//package com.java2s;

public class Main {

    public static void removeAllPrevSpace(StringBuilder val, int endInd) {
        int startInd = getLastNonSpaceInd(val, endInd);
        val.delete(startInd + 1, endInd + 1);
    }//from w  w  w  .  j a va2s  .  c  om

    public static int getLastNonSpaceInd(CharSequence val, int startInd) {
        int res = startInd;
        while (0 <= res) {
            char c = val.charAt(res);
            if (!Character.isSpaceChar(c) && !Character.isWhitespace(c))
                break;
            --res;
        }
        return res;
    }

    public static int getLastNonSpaceInd(CharSequence val) {
        return getLastNonSpaceInd(val, val.length() - 1);
    }
}

Related Tutorials