#1608 extending sys::Str with replaceMatch/replaceMatches

peter Mon 8 Aug 2011

I would like to propose sys:Str gain two additional methods: replaceMatch and replaceMatches. These would replace parts of a string based on a regular expression. e.g.:

"a1b2c3".replaceMatch  (Regex <|\d|>, "-")  => "a-b2c3"   
"a1b2c3".replaceMatches(Regex <|\d|>, "-")  => "a-b-c-"

and parallel the existing:

"a1b2c3".replace ("2", "-") => "a1b-c3"

What do people think?

(It seems sys::Str can only be extended using native methods - I can provide a Java version of these if needed. Currently, I have a Fantom implementation as static methods in a separate pod.)

brian Mon 8 Aug 2011

Would it make more sense to have those be methods on Regex?

rfeldman Mon 8 Aug 2011

I will say it's been convenient on several occasions to use Java's .replaceFirst and .replaceAll equivalents on String (and as an aside, the Java names for those methods make more intuitive sense to me).

kevinpeno Mon 8 Aug 2011

I think it makes sense for Str.replace to accept Regex as the from argument. This would mimic how javascript's str.replace works.

peter Wed 10 Aug 2011

Brian, I don't mind if they are on Regex instead. It feels more natural to me to be Str, but either is fine.

I do prefer rfeldman's suggestion of replaceFirst and replaceAll though, for the names. I hadn't looked at the Java APIs for this.

brian Tue 15 Nov 2011

Promoted to ticket #1608 and assigned to brian

brian Tue 15 Nov 2011

Ticket resolved in 1.0.61

Given the current design, I decided not to mix Regex stuff directly into the Str class. But I did add replaceFirst and replaceAll methods on RegexMatcher.

Login or Signup to reply.