Trim characters in Java

JavaStringTrim

Java Problem Overview


How can I trim characters in Java?
e.g.

String j = “\joe\jill\”.Trim(new char[] {“\”});

j should be

> "joe\jill"

String j = “jack\joe\jill\”.Trim("jack");

j should be

> "\joe\jill"

etc

Java Solutions


Solution 1 - Java

Apache Commons has a great StringUtils class (org.apache.commons.lang.StringUtils). In StringUtils there is a strip(String, String) method that will do what you want.

I highly recommend using Apache Commons anyway, especially the Collections and Lang libraries.

Solution 2 - Java

This does what you want:

public static void main (String[] args) {
	String a = "\\joe\\jill\\";
	String b = a.replaceAll("\\\\$", "").replaceAll("^\\\\", "");
	System.out.println(b);
}

The $ is used to remove the sequence in the end of string. The ^ is used to remove in the beggining.

As an alternative, you can use the syntax:

String b = a.replaceAll("\\\\$|^\\\\", "");

The | means "or".

In case you want to trim other chars, just adapt the regex:

String b = a.replaceAll("y$|^x", ""); // will remove all the y from the end and x from the beggining

Solution 3 - Java

CharMatcher – Google Guava

In the past, I'd second Colins’ Apache commons-lang answer. But now that Google’s guava-libraries is released, the CharMatcher class will do what you want quite nicely:

String j = CharMatcher.is('\\').trimFrom("\\joe\\jill\\"); 
// j is now joe\jill

CharMatcher has a very simple and powerful set of APIs as well as some predefined constants which make manipulation very easy. For example:

CharMatcher.is(':').countIn("a:b:c"); // returns 2
CharMatcher.isNot(':').countIn("a:b:c"); // returns 3
CharMatcher.inRange('a', 'b').countIn("a:b:c"); // returns 2
CharMatcher.DIGIT.retainFrom("a12b34"); // returns "1234"
CharMatcher.ASCII.negate().removeFrom("a®¶b"); // returns "ab";

Very nice stuff.

Solution 4 - Java

Here is another non-regexp, non-super-awesome, non-super-optimized, however very easy to understand non-external-lib solution:

public static String trimStringByString(String text, String trimBy) {
	int beginIndex = 0;
	int endIndex = text.length();
	
    while (text.substring(beginIndex, endIndex).startsWith(trimBy)) {
    	beginIndex += trimBy.length();
    } 
    	
    while (text.substring(beginIndex, endIndex).endsWith(trimBy)) {
    	endIndex -= trimBy.length();
    }
	
    return text.substring(beginIndex, endIndex);
}

Usage:

String trimmedString = trimStringByString(stringToTrim, "/");

Solution 5 - Java

You could use removeStart and removeEnd from Apache Commons Lang StringUtils

Solution 6 - Java

Hand made for the first option:

public class Rep {
    public static void main( String [] args ) {
       System.out.println( trimChar( '\\' , "\\\\\\joe\\jill\\\\\\\\" )  ) ;
       System.out.println( trimChar( '\\' , "joe\\jill" )  ) ;
    }
    private static String trimChar( char toTrim, String inString ) { 
        int from = 0;
        int to = inString.length();

        for( int i = 0 ; i < inString.length() ; i++ ) {
            if( inString.charAt( i ) != toTrim) {
                from = i;
                break;
            }
        }
        for( int i = inString.length()-1 ; i >= 0 ; i-- ){ 
            if( inString.charAt( i ) != toTrim ){
                to = i;
                break;
            }
        }
        return inString.substring( from , to );
    }
}

Prints

joe\jil

joe\jil

Solution 7 - Java

public static String trim(String value, char c) {

    if (c <= 32) return value.trim();

    int len = value.length();
    int st = 0;
    char[] val = value.toCharArray();    /* avoid getfield opcode */

    while ((st < len) && (val[st] == c)) {
        st++;
    }
    while ((st < len) && (val[len - 1] == c)) {
        len--;
    }
    return ((st > 0) || (len < value.length())) ? value.substring(st, len) : value;
}

Solution 8 - Java

EDIT: Amended by answer to replace just the first and last '' character.

System.err.println("\\joe\\jill\\".replaceAll("^\\\\|\\\\$", ""));

Solution 9 - Java

I don't think there is any built in function to trim based on a passed in string. Here is a small example of how to do this. This is not likely the most efficient solution, but it is probably fast enough for most situations, evaluate and adapt to your needs. I recommend testing performance and optimizing as needed for any code snippet that will be used regularly. Below, I've included some timing information as an example.

public String trim( String stringToTrim, String stringToRemove )
{
    String answer = stringToTrim;

    while( answer.startsWith( stringToRemove ) )
    {
        answer = answer.substring( stringToRemove.length() );
    }

    while( answer.endsWith( stringToRemove ) )
    {
        answer = answer.substring( 0, answer.length() - stringToRemove.length() );
    }

    return answer;
}

This answer assumes that the characters to be trimmed are a string. For example, passing in "abc" will trim out "abc" but not "bbc" or "cba", etc.

Some performance times for running each of the following 10 million times.

" mile ".trim(); runs in 248 ms included as a reference implementation for performance comparisons.

trim( "smiles", "s" ); runs in 547 ms - approximately 2 times as long as java's String.trim() method.

"smiles".replaceAll("s$|^s",""); runs in 12,306 ms - approximately 48 times as long as java's String.trim() method.

And using a compiled regex pattern Pattern pattern = Pattern.compile("s$|^s"); pattern.matcher("smiles").replaceAll(""); runs in 7,804 ms - approximately 31 times as long as java's String.trim() method.

Solution 10 - Java

it appears that there is no ready to use java api that makes that but you can write a method to do that for you. this link might be usefull

Solution 11 - Java

I would actually write my own little function that does the trick by using plain old char access:

public static String trimBackslash( String str )
{
    int len, left, right;
    return str == null || ( len = str.length() ) == 0 
                           || ( ( left = str.charAt( 0 ) == '\\' ? 1 : 0 ) |
           ( right = len > left && str.charAt( len - 1 ) == '\\' ? 1 : 0 ) ) == 0
        ? str : str.substring( left, len - right );
}

This behaves similar to what String.trim() does, only that it works with '\' instead of space.

Here is one alternative that works and actually uses trim(). ;) Althogh it's not very efficient it will probably beat all regexp based approaches performance wise.

String j = “\joe\jill\”;
j = j.replace( '\\', '\f' ).trim().replace( '\f', '\\' );

Solution 12 - Java

Here's how I would do it.

I think it's about as efficient as it reasonably can be. It optimizes the single character case and avoids creating multiple substrings for each subsequence removed.

Note that the corner case of passing an empty string to trim is handled (some of the other answers would go into an infinite loop).

/** Trim all occurrences of the string <code>rmvval</code> from the left and right of <code>src</code>.  Note that <code>rmvval</code> constitutes an entire string which must match using <code>String.startsWith</code> and <code>String.endsWith</code>. */
static public String trim(String src, String rmvval) {
    return trim(src,rmvval,rmvval,true);
    }

/** Trim all occurrences of the string <code>lftval</code> from the left and <code>rgtval</code> from the right of <code>src</code>.  Note that the values to remove constitute strings which must match using <code>String.startsWith</code> and <code>String.endsWith</code>. */
static public String trim(String src, String lftval, String rgtval, boolean igncas) {
    int                                 str=0,end=src.length();

    if(lftval.length()==1) {                                                    // optimize for common use - trimming a single character from left
        char chr=lftval.charAt(0);
        while(str<end && src.charAt(str)==chr) { str++; }
        }
    else if(lftval.length()>1) {                                                // handle repeated removal of a specific character sequence from left
        int vallen=lftval.length(),newstr;
        while((newstr=(str+vallen))<=end && src.regionMatches(igncas,str,lftval,0,vallen)) { str=newstr; }
        }

    if(rgtval.length()==1) {                                                    // optimize for common use - trimming a single character from right
        char chr=rgtval.charAt(0);
        while(str<end && src.charAt(end-1)==chr) { end--; }
        }
    else if(rgtval.length()>1) {                                                // handle repeated removal of a specific character sequence from right
        int vallen=rgtval.length(),newend;
        while(str<=(newend=(end-vallen)) && src.regionMatches(igncas,newend,rgtval,0,vallen)) { end=newend; }
        }

    if(str!=0 || end!=src.length()) {
        if(str<end) { src=src.substring(str,end); }                            // str is inclusive, end is exclusive
        else        { src="";                     }
        }

    return src;
    }

Solution 13 - Java

10 year old question but felt most of the answers were a bit convoluted or didn't quite work the way that was asked. Also the most upvoted answer here didn't provide any examples. Here's a simple class I made:

https://gist.github.com/Maxdw/d71afd11db2df4f1297ad3722d6392ec

Usage:

Trim.left("\joe\jill\", "\") == "joe\jill\"

Trim.left("jack\joe\jill\", "jack") == "\joe\jill\"

Trim.left("\\\\joe\\jill\\\\", "\") == "joe\\jill\\\\"

Solution 14 - Java

My solution:

private static String trim(String string, String charSequence) {
        var str = string;
        str = str.replace(" ", "$SAVE_SPACE$").
                  replace(charSequence, " ").
                  trim().
                  replace(" ", charSequence).
                  replace("$SAVE_SPACE$", " ");
        return str;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQuintin ParView Question on Stackoverflow
Solution 1 - JavaColin GislasonView Answer on Stackoverflow
Solution 2 - JavaPaulo GuedesView Answer on Stackoverflow
Solution 3 - JavaCowanView Answer on Stackoverflow
Solution 4 - Javajake_hetfieldView Answer on Stackoverflow
Solution 5 - JavaValentin RocherView Answer on Stackoverflow
Solution 6 - JavaOscarRyzView Answer on Stackoverflow
Solution 7 - JavailwView Answer on Stackoverflow
Solution 8 - JavaAdamskiView Answer on Stackoverflow
Solution 9 - JavaAlex BView Answer on Stackoverflow
Solution 10 - JavaAhmed KotbView Answer on Stackoverflow
Solution 11 - Javax4uView Answer on Stackoverflow
Solution 12 - JavaLawrence DolView Answer on Stackoverflow
Solution 13 - JavaMaxView Answer on Stackoverflow
Solution 14 - JavaDavidView Answer on Stackoverflow