After JDK 1.4, the string object calls the public String replaceAll(String regex, String replacement) method to return a string. This string is the substring in the current string that matches the regular expression specified by the parameter regex. The string replaced by the string specified by replacement, for example:
Strings=123hel1o456bird.replaceAll([a-zA-Z]+, hello);
Then s is the string obtained by replacing all the English substrings in 123hello456bird with hello, that is, s is 123hello456hello.
Note : Calling the replaceAll() method on the current string returns a string, but does not change the current string.
After JDK 1.4, the String class provides a practical method public String[] split(String regex) . When calling this method on a string, the regular expression regex specified by the parameter is used as a delimiter to decompose the words in it, and the words will be decomposed The resulting words are stored in a string array. For example, for the string str:
Stringstr=October 1, 1949 is the day when the People's Republic of China was founded;
If you want to decompose words that consist entirely of numeric characters, you must use non-numeric strings as delimiters. Therefore, the regular expression String regex=\D+ can be used as a delimiter to break out the words in str:
StringdigitWord[]=str.split(regex);
Then, digitWord[0], digitWord[1], and digitWord[2] are 1949, 10, and 1 respectively.