Results 1 to 5 of 5
Thread: The meaning of replaceAll
- 10-07-2011, 04:16 AM #1
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
- 10-07-2011, 04:22 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: The meaning of replaceAll
What don't you understand?
- 10-07-2011, 04:41 AM #3
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Re: The meaning of replaceAll
I don't understand how to use replaceAll , you can help me ?
- 10-07-2011, 05:14 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: The meaning of replaceAll
The replace all methods api documentation states:
The documentation should be fairly clear, but just in case here is a brief explanation. Strings are immutable, therefore replaceAll returns a changed string. It doesn't change the string it is called on, but it uses the string it is called on as the return value. The first argument in the replaceAll method is the 'to find' string, which can also be a regex. Regex stands for regular expression, and is a broad topic -- you can find full books on it. So the replaceAll method takes this regex and searches the string which it is called on and finds all occurrences. For each occurrence of the regex, it replaces it with the second argument. I'm thinking your confusion stems from the regex in this method call.Java Code:public String replaceAll(String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement. An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl) Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired. Parameters: regex - the regular expression to which this string is to be matched replacement - the string to be substituted for each match Returns: The resulting String
Play around with replaceAll on your own to see if you can understand it more. Here is an example:
Java Code:String s = "Hello there, hello, how are you? hello Hello, hello"; String k = s.replaceAll("Hello", "Goodbye"); String f = s.replaceAll("hello", "goodbye");
- 10-07-2011, 05:18 AM #5
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Problem with replaceAll
By Timmy in forum New To JavaReplies: 6Last Post: 06-06-2011, 04:51 PM -
String.replaceAll(str, str) is leading to infinite loop. Help?
By TheNadz in forum New To JavaReplies: 2Last Post: 04-25-2011, 04:13 PM -
replaceAll Problem
By steve_m in forum New To JavaReplies: 3Last Post: 12-22-2010, 01:09 PM -
Regular Expressions and String.replaceAll()
By meta1203 in forum New To JavaReplies: 1Last Post: 11-24-2010, 11:41 PM -
Exception:Meaning
By tiger100plus in forum New To JavaReplies: 2Last Post: 11-27-2008, 07:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks