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

后端 未结 12 1909
陌清茗
陌清茗 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: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.

提交回复
热议问题