• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Question about String.replace()

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic