Trim spaces in CharBuffer and tabs moving left from right - Java java.nio

Java examples for java.nio:CharBuffer

Description

Trim spaces in CharBuffer and tabs moving left from right

Demo Code


//package com.java2s;
import java.nio.CharBuffer;

public class Main {
    /**//from   ww  w.j  a  v a2s . c  o  m
     * Trim spaces and tabs moving left from toExc.  Do not go beyond from.
     *
     * @return the position after removing whitespace, toExc if no whitespace was found
     */
    public static int trimRight(CharBuffer buf, int from, int toExc) {
        for (int i = toExc - 1; i >= from; i--) {
            char c = buf.get(i);

            if (c != ' ' && c != '\t') {
                return i + 1;
            }
        }

        return toExc;
    }
}

Related Tutorials