Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace a character in a string in Java?

Tags:

java

replace

Using Java, I want to go through the lines of a text and replace all ampersand symbols (&) with the XML entity reference &.

I scan the lines of the text and then each word in the text with the Scanner class. Then I use the CharacterIterator to iterate over each characters of the word. However, how can I replace the character? First, Strings are immutable objects. Second, I want to replace a character (&) with several characters(amp&;). How should I approach this?

CharacterIterator it = new StringCharacterIterator(token); for(char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {        if(ch == '&') {         } } 
like image 377
user42155 Avatar asked Aug 05 '09 17:08

user42155


People also ask

How do I replace a character in a string?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.

How do you replace a single character in Java?

String are immutable in Java. You can't change them. You need to create a new string with the character replaced.

How do you replace a character in a string in Java without using replace method?

To replace a character in a String, without using the replace() method, try the below logic. Let's say the following is our string. int pos = 7; char rep = 'p'; String res = str. substring(0, pos) + rep + str.

How do you update a string in Java?

Strings are immutable. Once you have created a string you cannot later change that string object. Java uses pass-by-value, not pass-by-reference. When you assign a new value to s in your method it only modifies the local s , not the original s in the calling code.


2 Answers

Try using String.replace() or String.replaceAll() instead.

String my_new_str = my_str.replace("&", "&"); 

(Both replace all occurrences; replaceAll allows use of regex.)

like image 82
Amber Avatar answered Oct 05 '22 23:10

Amber


The simple answer is:

token = token.replace("&", "&"); 

Despite the name as compared to replaceAll, replace does do a replaceAll, it just doesn't use a regular expression, which seems to be in order here (both from a performance and a good practice perspective - don't use regular expressions by accident as they have special character requirements which you won't be paying attention to).

Sean Bright's answer is probably as good as is worth thinking about from a performance perspective absent some further target requirement on performance and performance testing, if you already know this code is a hot spot for performance, if that is where your question is coming from. It certainly doesn't deserve the downvotes. Just use StringBuilder instead of StringBuffer unless you need the synchronization.

That being said, there is a somewhat deeper potential problem here. Escaping characters is a known problem which lots of libraries out there address. You may want to consider wrapping the data in a CDATA section in the XML, or you may prefer to use an XML library (including the one that comes with the JDK now) to actually generate the XML properly (so that it will handle the encoding).

Apache also has an escaping library as part of Commons Lang.

like image 45
Yishai Avatar answered Oct 06 '22 00:10

Yishai