Java's built-in String.trim() method trims leading and trailing whitespace, but only looks for whitespace characters within ASCII ranges. - Java java.lang

Java examples for java.lang:char

Description

Java's built-in String.trim() method trims leading and trailing whitespace, but only looks for whitespace characters within ASCII ranges.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String s = "java2s.com";
        System.out.println(trimInternationally(s));
    }/*from  w w w . ja v  a  2 s  .co m*/

    /**
     * Java's built-in String.trim() method trims leading and trailing whitespace, but only
     * looks for whitespace characters within ASCII ranges. Instead, we want to trim all the
     * whitespace from Unicode ranges.
     */
    public static String trimInternationally(String s) {
        if (s == null)
            return null;
        if (s.length() == 0)
            return s;
        int leadingOffset = 0;
        while (leadingOffset < s.length()
                && Character.isWhitespace(s.charAt(leadingOffset)))
            leadingOffset++;
        if (leadingOffset == s.length())
            return ""; // No non-whitespace characters found. Return empty string
        int trailingOffset = s.length() - 1;
        while (Character.isWhitespace(s.charAt(trailingOffset)))
            trailingOffset--;
        return s.substring(leadingOffset, trailingOffset + 1);
    }
}

Related Tutorials