commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scovetta, Michael V" <>
Subject [Lang] ReplaceAll like String.ReplaceAll, but no regexs
Date Wed, 16 Mar 2005 17:30:36 GMT
   I'm looking for a clean way of replacing all instances of a substring
with another. Sounds easy, right? Here's my desired functionality:

	MyReplaceAll("abc", "a", "z")    - zbc
	MyReplaceAll("abc", "a", "")     - bc
	MyReplaceAll("$1$2$3", "$", "b") - b1b2b3
	MyReplaceAll("abc", null, "foo") - abc
	MyReplaceAll("abc", "foo", null) - abc

String.replaceAll interprets the replacement values as regular
expressions, and chokes on $-signs. StringUtils.replace is **almost**
what I'm looking for, but (as per the javadocs):

	StringUtils.replace("abaa", "a", "", 1)    = "abaa"

I would expect replacing the String "a" with a blank string would result

	StringUtils.replace("abaa", "a", "", 1)    = "baa" (max of one
replacement specified)

Am I just missing something here? Is there another function I should use
to "de-regex" my input strings, or have you guys found a better way of
doing this?

Thanks for your help in advance!

Michael Scovetta
Computer Associates
Senior Application Developer

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message