Remove first word from a string in Java

前端 未结 9 1889
天涯浪人
天涯浪人 2020-12-29 12:20

What\'s the best way to remove the first word from a string in Java?

If I have

String originalString = \"This is a string\";

I want to r

相关标签:
9条回答
  • 2020-12-29 13:01

    This will definitely a good solution

        String originalString = "This is a string";
        originalString =originalString.replaceFirst("This ", "");
    
    0 讨论(0)
  • 2020-12-29 13:02
    String str = "This is a string";    
    
    String str2=str.substring(str.indexOf(" "));
    String str3=str.replaceFirst(str2, "");
    

    String's replaceFirst and substring

    0 讨论(0)
  • 2020-12-29 13:09

    also you can use this solution:

    static String substringer(String inputString, String remove) {
        if (inputString.substring(0, remove.length()).equalsIgnoreCase(remove)) {
            return inputString.substring(remove.length()).trim();
        }
        else {
                return inputString.trim();
        }
    }
    

    Example : substringer("This is a string", "This");

    0 讨论(0)
提交回复
热议问题