• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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: 13089
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: 23942
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
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic