Results 1 to 5 of 5
  1. #1
    herolua is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default The meaning of replaceAll

    return Kq.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " "); --> I do not understand this code . Please Help me (T.T)

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: The meaning of replaceAll

    What don't you understand?

  3. #3
    herolua is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default Re: The meaning of replaceAll

    I don't understand how to use replaceAll , you can help me ?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: The meaning of replaceAll

    The replace all methods api documentation states:

    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
    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.

    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");

  5. #5
    herolua is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default Re: The meaning of replaceAll

    Thank you very much , I understood what is it ? oh yeah , I will start next step

Similar Threads

  1. Problem with replaceAll
    By Timmy in forum New To Java
    Replies: 6
    Last Post: 06-06-2011, 05:51 PM
  2. Replies: 2
    Last Post: 04-25-2011, 05:13 PM
  3. replaceAll Problem
    By steve_m in forum New To Java
    Replies: 3
    Last Post: 12-22-2010, 02:09 PM
  4. Regular Expressions and String.replaceAll()
    By meta1203 in forum New To Java
    Replies: 1
    Last Post: 11-25-2010, 12:41 AM
  5. Exception:Meaning
    By tiger100plus in forum New To Java
    Replies: 2
    Last Post: 11-27-2008, 08:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •