17

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 getting replaced. Can anyone please tell what I need to change in the following code in the line number
(I referred the code from here How do I delete specific characters from a particular String in Java?)

public static void main(String[] args) {
    String text = "wherabouts;";
    System.out.println("SSS "+text.substring(text.length()-1));
    if(text.substring(text.length()-1) == ";"){
        text.replaceAll(";", "");
    }
    System.out.println("TEXT : "+text);
}
Community
  • 1
  • 1
user1732653
  • 627
  • 3
  • 8
  • 18

12 Answers12

26
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(";+$", "");
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
18

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.

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • what s the $ sign at the end for? regex? endswith? – DarthVader Oct 09 '12 at 18:39
  • 1
    Correct, the `$` symbol represents _the end of input_ in a regex. This typically helps to match a pattern that appears at the end of a line or the end of a string. This causes `text.replaceAll(";$", "")` to replace only a semi-colon that appears as the last character. – jahroy Oct 09 '12 at 18:40
  • Note that as @RohitJain has pointed out, if you want to deal with the possibility of multiple semi-colons at the end of the line, you can add a `+` symbol: `text.replaceAll(";+$", "");` – jahroy Oct 09 '12 at 19:04
4
text = text.replaceAll(";", "");

Here's a little extra reading for you http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
3

You should not forget that String is immutable. So, whenever you want to modify it, you have to assign the result to a variable.

A possible solution to what you need:

if (text.endsWith(";") {
  text = text.substring(0, text.length() - 1);
}
Dan D.
  • 32,246
  • 5
  • 63
  • 79
2

Strings in java are immutable, so replaceAll returns a new string.

Do

 text = text.replaceAll(";", "");
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
2

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(";", "");
asteri
  • 11,402
  • 13
  • 60
  • 84
2
if (text.endsWith(";")){
    text = text.substring(0,text.length()-1);
}
ewok
  • 20,148
  • 51
  • 149
  • 254
2

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

text.substring(0, text.length() - 1)
Reimeus
  • 158,255
  • 15
  • 216
  • 276
2

String is immutable so new String will be created after replace.

String newString = text.replace(";", "");

or

String newString = text.replaceAll(";$", "");
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

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(";", "");
Community
  • 1
  • 1
Nivas
  • 18,126
  • 4
  • 62
  • 76
1
public static void main(String[] args) {
    String text_original = "wherabouts;";
    char[] c = text_original.toCharArray();

    System.out.println("TEXT original: "+ text_original);

    String text_new = c[text_original.length()-1] == ';' ? text_original.substring(0,text_original.length()-2) : text_original;

    System.out.println("TEXT new: "+text_new);
}
fycth
  • 3,410
  • 25
  • 37
1

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(";$", "");
Cory Kendall
  • 7,195
  • 8
  • 37
  • 64