How to remove first word from String in Java

Let’s see different ways to remove the first word from String in Java.

Remove the first word from String in Java using String’s substring() method

String originalString = "Hello world!";
String newString = originalString.substring(originalString.indexOf(" ") + 1);

This code takes the original string “Hello world!” and uses the indexOf() method to find the index of the first space character in the string (which is at index 5). It then adds 1 to that index to get the starting index of the substring we want to extract (which is index 6). Finally, it uses the substring() method to extract the portion of the string starting at index 6 and ending at the end of the string, which is the word “world”

Using String’s join() method

String originalString = "Hello world!";
String[] words = originalString.split(" ");
String newString = String.join(" ", Arrays.copyOfRange(words, 1, words.length));

Using String replaceFirst() method

String originalString = "Hello world!";
String newString = originalString.replaceFirst("[^ ]+ ", "");

Another example

String originalString = "Hello world!";
String newString = originalString.replaceFirst("\\b\\w+\\b", "");

Using String split() method

String originalString = "Hello world!";
System.out.println(Arrays.toString(originalString .split(" ", 2)));

The “best” way to remove the first word from a String in Java depends on the specific requirements of your use case. However, I can provide some guidelines to help you choose the most appropriate method:

  • Method 1 (substring()): This method is simple and straightforward. However, it assumes that the input string contains at least one space character, and it does not handle the case where the input string is empty. If you know for sure that the input string will always contain a space character and will not be empty, then this method is a good choice.
  • Method 2 (split()): This method is more robust than Method 1, as it can handle cases where the input string is empty or does not contain a space character. However, it creates an array of strings and then joins them again, which may not be the most efficient solution for large strings.
  • Method 3 (replaceFirst()): This method uses regular expressions to remove the first word of the string. It is a concise solution that can handle edge cases well. However, it may be slower than some of the other methods due to the regular expression processing.
  • Method 4 (replace() with regular expression): This method is similar to Method 3, but it replaces all occurrences of the first word in the string rather than just the first occurrence. It is a good choice if you want to remove all occurrences of a specific word in a string.
  • Method 5 (substring() with indexOf() and length()): This method is similar to Method 1, but it is more verbose. However, it may be slightly more efficient than Method 1 because it avoids creating a new string object.

In general, Method 2 is a good choice if you need a robust solution that can handle edge cases. Methods 3 and 4 are good choices if you need to remove a specific word rather than just the first word. Method 1 or Method 5 may be a good choice if you know for sure that the input string will always contain a space character and will not be empty, and you want a simple and straightforward solution.

That’s all about How to remove first word from String in Java.

Related post.