How to remove a prefix from a String in Java

In this post, we will see how to remove a prefix from a String in Java.

 

1. String.substring()

The standard solution is to make use of substring() method of the String class as shown below:

 

Download   Run Code

Output:

natural

 

2. String.split()

Another plausible way is to split the given string around given prefix using split() method of the String class and return the second string value of the array.

 

Download   Run Code

Output:

natural

 

3. Apache Commons StringUtils

We can also use Apache Commons Lang library which has removeStart() utility method in StringUtils class which does the job and also handles NullPointerException.

 

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of