• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about String.replace()  RSS feed

 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

Thanks for reading my question. According to String.java in 5, the implementation for String.replace(CharSequence, CharSequence) is



while the implementation for String.replace( char, char ) is



I don't quite understand how Pattern and Matcher object work, my question is does the iterate the entire string and replace the char just like

Thanks
Davie
 
fred rosenberger
lowercase baba
Bartender
Posts: 12558
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the API:
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".


Of course, you could always write a quick program to test various cases and see for yourself what it does.
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Davie Lin wrote:
Thanks for reading my question. According to String.java in 5, the implementation for String.replace(CharSequence, CharSequence) is



I don't quite understand how Pattern and Matcher object work, my question is does the iterate the entire string and replace the char just like



Well, we are not going to explain regular expressions here .... there are whole books dedicated to the subject. It is arguably a language itself.

Basically, Java has a regular expression engine as part of the core libraries -- and the Pattern and Matcher classes is how you use it. As for the LITERAL and quoteReplacement() stuff, that is used to disable practically all the special features of the regular expression engine.... basically, it is using the regular expression engine to do simple string replacement.

Henry
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:According to the API:
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".


Of course, you could always write a quick program to test various cases and see for yourself what it does.


That sounds to me like the API is saying it check the literal target one at a time, thus iterate the entire string if the literal target is at the end of the String.
Does that sound right?

Thanks
Davie
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Davie Lin wrote:That sounds to me like the API is saying it check the literal target one at a time, thus iterate the entire string if the literal target is at the end of the String. Does that sound right?

Sounds about right; and if you want to replace every occurrence of a substring, I can't imagine how it could do otherwise.

String does have a method called lastIndexOf() if you're only interested in the last match, and I suspect it works from the right-hand end. It doesn't uses regexes though.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!