Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove characters from a String in Java

Tags:

java

string

People also ask

How do I remove a specific character from a string?

You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement. The following example removes all commas from a string.

How do I remove the first 10 characters from a string in Java?

You can use the substring() method of java. lang. String class to remove the first or last character of String in Java. The substring() method is overloaded and provides a couple of versions that allows you to remove a character from any position in Java.

How do I remove something from a string in Java?

Use the deleteCharAt Method to Remove a Character From String in Java. The deleteCharAt() method is a member method of the StringBuilder class that can also be used to remove a character from a string in Java.

Can you remove from a string in Java?

No, because Strings in Java are immutable.


Strings in java are immutable. That means you need to create a new string or overwrite your old string to achieve the desired affect:

id = id.replace(".xml", "");

Can't you use

id = id.substring(0, id.length()-4);

And what Eric said, ofcourse.


Strings are immutable, so when you manipulate them you need to assign the result to a string:

String id = fileR.getName();
id = id.replace(".xml", ""); // this is the key line
idList.add(id);

Strings are immutable. Therefore String.replace() does not modify id, it returns a new String with the appropriate value. Therefore you want to use id = id.replace(".xml", "");.


String id = id.substring(0,id.length()-4)