return Kq.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " "); --> I do not understand this code . Please Help me (T.T)
Printable View
return Kq.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " "); --> I do not understand this code . Please Help me (T.T)
What don't you understand?
I don't understand how to use replaceAll , you can help me ?
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.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:
Code:String s = "Hello there, hello, how are you? hello Hello, hello";
String k = s.replaceAll("Hello", "Goodbye");
String f = s.replaceAll("hello", "goodbye");
Thank you very much , I understood what is it ? oh yeah , I will start next step