java - removing semi colon from a string if the string ends with it

后端 未结 12 1690
陌清茗
陌清茗 2020-12-29 08:20

I have a requirement in which I need to remove the semicolon if it is present at the end of the String(only at the end). I have tried the following code. But still it is not

相关标签:
12条回答
  • 2020-12-29 09:18

    There are multiple problems in the code you have provided.

    Use equals to compare objects.

    if(text.substring(text.length()-1).equals(";"))
    

    If you want to replace only the last character, you dont need replaceAll.

    So do either

    if(text.substring(text.length()-1).equals(";")) {
          text = text.substring(0, text.length()-1);
        } 
    

    or

    text = text.replaceAll(";", "");
    
    0 讨论(0)
  • 2020-12-29 09:19
    text.replaceAll(";", "");
    

    Since Strings in Java are immutable, so replaceALl() method doesn't do the in-place replacement, rather it returns a new modified string. So, you need to store the return value in some other string. Also, to match the semi-colon at the end, you need to use $ quantifier after ;

    text = text.replaceAll(";$", "");
    

    $ denotes the end of the string since you want to replace the last semi-colon..

    If you don't use $, it will replace all the ; from your strings..

    Or, for your job, you can simply use this, if you want to remove the last ;:

        if (text.endsWith(";")) {
            text = text.substring(0, text.length() - 1);
            System.out.println(text);
        }
    

    UPDATE: And, if there are more semi-colons at the end:

    text = text.replaceAll(";+$", "");
    
    0 讨论(0)
  • 2020-12-29 09:19

    String modifiedText = text.replaceAll(";$", "");

    OR

    text = text.replaceAll(";$", "");

    OR

    if (text.endsWith(";")) {
        text = text.substring(0, text.length() - 1);
    }
    

    NOTE:

    Strings are immutable. That means you can't change them.

    Therefore you have to either re-assign text or set it to a new variable.

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

    You are using the wrong version of String.substring, You could use:

    text.substring(0, text.length() - 1)
    
    0 讨论(0)
  • 2020-12-29 09:22

    Use .equals() instead of == if you're going to use substring() rather than charAt():

    if(text.substring(text.length()-1).equals(";"))
    

    Also, reassign your text variable:

    text = text.replaceAll(";", "");
    
    0 讨论(0)
  • 2020-12-29 09:22

    Solution for only one semi-colon

    // Don't use regular expressions if you don't need to.
    if (text.endsWith(";")) {
        text = text.substring(0, text.length() - 1);
    }
    

    Slower solution for possibly more than one semi-colon

    text.replaceAll(";+$", "");
    

    Additionally, here are some other problems with the code you originally posted, for reference.

    if(text.substring(text.length()-1) == ";"){
    

    You can't compare strings with ==. Instead, you have to compare them with .equals(). This would be correctly written like this ...ength()-1).equals(";").

    text.replaceAll(";", "");
    

    This replaces all semicolons it finds. This means that if your string was some;thing;, it would turn it into something, but you want to only remove the last semicolon, like this: some;thing. To do this correctly, you need to look for the end of the string, using the special $ character like this:

    text.replaceAll(";$", "");
    
    0 讨论(0)
提交回复
热议问题