Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to replace and decompose character sequences in Java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to replace and decompose the character sequence in Java", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve character sequence replacement and decomposition in Java" article.

Using the String class

The String object calls the public String replaceAll (String regex,String replacement) method and returns a new String object. The character sequence of the returned String object is the character sequence obtained by replacing all the subcharacter sequences matching with the parameter regex in the character sequence of the current String object with the character sequence specified by the parameter replacement.

For example:

String S1 = "123hello456"; String s2=s1.replaceAll ("\\ d +", "Hello.") / / "\\ d +" is a regular expression that indicates any number System.out.println (S1) between one or more 09s; / / the print result is: 123hello456 has not been changed System.out.println (S2); / / the print result is: Hello. Hello, hello.

Another example is:

String regex= "-? [0-9] [0-9] * [.]? [0-9] *"; String S1 = "999 Hello,-123.459804 has a holiday tomorrow"; String s2=s1.replaceAll (regex, "); System.out.println (" excluding the number in "+ S1 +", the character sequence is: "+ S2); / / excluding 999Hello everyone,-123.459804 after tomorrow's holiday, the character sequence is: Hello, everyone has a holiday tomorrow.

In fact, the String class provides a practical method:

Public String [] split (String regex)

When the method is called by the String object, the regular expression regex specified by the parameter is used as a separator to decompose the words in the character sequence of the String object and store the decomposed words in the String array.

For example:

/ / requirement: for a character sequence, it is necessary to decompose all words made up of numeric characters. String S1 = "on the evening of September 18, 1931, Japan launched a war of aggression against China, please remember this date!" ; String regex= "\ D+"; String S2 [] = s1.split (regex); for (String s:s2) System.out.println (s); / / output 1931 09 08 respectively, and s2.length () = 3

It should be noted that the split method believes that the left and right sides of the separation mark are words, and the additional rule is that if the word on the left is a character sequence that does not contain any characters, that is, empty, then the character sequence still counts as a word, but the word on the right must be a character sequence containing characters.

For example:

String S1 = "February 18, 2022"; String regex= "\ D+"; String S2 [] = s1.split (regex); System.out.println (s2.length ()); / / compile error: Method call expected for (String s:s2) System.out.println (s); / / S2 [0] = S2 [1] = 2022 S2 [2] = 02 S2 [3] = 18 S1 [0] is an empty string and nothing is displayed. / / so the length of the S2 array should be 4 instead of 3, and the extra empty string is "AD" with a word on the left by default. The content is empty. Second, use StringTokenizer class

1. Unlike the split () method, StringTokenizer objects are not delimited with regular expressions.

two。 When analyzing a character sequence and decomposing it into words that can be used independently, you can use the StringTokenizer class in the java.util package, which calls the object of this class a character sequence parser. This class has two construction methods.

Constructor 1:StringTokenizer (String s): construct a StringTokenizer object, such as fenxi. Fenxi uses the default separator (space character, line feed character, carriage return character, Tab character, feed character (\ f)) to decompose the words in the character sequence of the parameter s, that is, these words become the data in the analysis.

Constructor 2:StringTokenizer (String sforce string delim): construct a StringTokenizer object, such as fenxi. Fenxi uses any arrangement of characters in the character sequence of parameter delim as a separator to decompose the words in the character sequence of parameter s, that is, these words become data in fenxi.

Note: any arrangement of delimited marks is still a delimited mark.

3.fenxi can call the String nextToken () method to get the words in fenxi one by one, and every time nextToken () returns a word, fenxi automatically deletes that word.

4.fenxi can call the boolean hasMoreTokens () method to return a Boolean value, which returns true as long as there are words in the fenxi, or false otherwise.

5.fenxi can call the countToken () method to return the number of words in the current fenxi.

Specific example 1:

String s = "we are stud,ents"; StringTokenizer fenxi=new StringTokenizer (s, ","); / / any combination of spaces and commas as the separator int number=fenxi.countToken (); while (fenxi.hasMoreTokens ()) {String str=fenxi.nextToken (); System.out.println (str); System.out.println ("left" + fenxi.countToken () + "word");} System.out.println ("s common word:" + number+ ") / / output result: 3 words left in we, 2 words left in are, 1 word left in stud, 0 words left in ents, 4 words in common

Specific example 2:

String s = "Local call fee: 28.39RMB, long distance call fee: 49.15RMB, Internet access fee: 352RMB"; String delim= "[^ 0-9.] +"; / / non-digital sum. All sequences match delims=s.replaceAll (delim, "#"); StringTokenizer fenxi=new StringTokenizer (s, "#"); double totalMoney=0;while (fenxi.hasMoreTokens ()) {double money=Double.parseDouble (fenxi.nextToken ()); System.out.println (money); totalMoney+=money;} System.out.println ("total cost:" + totalMoney+ "yuan"); / / output result: 28.3949.15352.0 total cost: 429.53999999999996 III, use Scanner class

To create a Scanner object, you need to pass a String object to the constructed Scanner object, for example, for:

String s = "telephone cost 876 dollar.Computer cost 2398.89 dollar."

To understand the numeric words in the character sequence that separates s, you can construct a Scanner object as follows:

Scanner scanner=new Scanner (s)

Then scanner defaults to using spaces as a separator to parse the words in the character sequence of s. You can also have the scanner object call the method:

UseDelimiter (regular expression)

The regular expression is used as the delimiter, that is, when the Scanner object parses the character sequence of s, the character sequence that matches the regular expression is used as the delimiter.

The characteristics of Scanner object parsing character sequences are as follows:

The scanner object calls the next () method to return the words in the character sequence of s in turn. If the last word has been returned by the next () method, the scanner object calls hasNext () to return false, otherwise it returns true.

For numeric words in the character sequence of s, for example, 12.34, scanner can call the nextInt () or nextDouble () method instead of the next () method. That is, scanner can call the nextInt () or nextDouble () method to convert numeric words into int or string data returns.

If the word is not a numeric word, scanner calls the nextInt () or nextDouble () method, and an InputMismatchException exception will occur. When handling the exception, you can call the next () method to return the non-digitized word.

Specific examples:

String cost=: 28.39RMB for local calls, RMB49.15for long-distance calls, RMB352for Internet access; Scanner scanner=new Scanner (cost); scanner.useDelimiter ("[^ 0-9.] +"); double sum=0;while (scanner.hasNext ()) {try {double price=scanner.nextDouble (); sum+=price; System.out.println (price);} catch (InputMismatchException e) {String s=scanner.next ();}} System.out.println ("Total cost:" + sum+ "yuan") / / output result: 28.3949.15352.0 Total cost: 429.5399999999999996

Contrast:

1. Both the StringTokenizer class and the Scanner class can be used to decompose words in a character sequence, but they are different in thought.

2. The StringTokenizer class puts all the decomposed words into the entity of the StringTokenizer object, so the StringTokenizer object can quickly obtain the words, that is, the entity of the StringTokenizer object takes up more memory (takes up more memory and improves the speed, which is equivalent to memorizing the words in the brain).

3. Unlike the StringTokenizer class, the Scanner class only holds delimited tags on how to get words, so scanner objects get words relatively slowly, but scanner objects save memory space (slow down, save space, which is equivalent to putting words in a dictionary, and the brain only remembers the rules of looking up dictionaries).

Fourth, use Pattern class and Matcher class

The steps to use the Pattern class and the Matcher class are as follows:

1. Use the regular expression regex as a parameter to get an instance of the Pattern class called "pattern" pattern. For example

String regex= "? [0-9] [0-9] * [.]? [0-9] *"; Pattern pattern=Pattern.compile (regex)

two。 The schema object pattern calls the matcher (CharSequence s) method to return a Matcher object matcher, called the matching object, and the parameter s is the String object to be retrieved by matcher.

Matcher matcher=pattern.matcher (s)

3. After these two steps are completed, the matching object matcher can call various methods to retrieve s.

The specific methods are:

(1) public boolean find (): find the next subsequence in the character sequence of s that matches regex. Returns true if successful, false otherwise. When matcher first calls this method, it looks for the first subsequence in s that matches regex. If the find method returns true, then when matcher calls the find method, it will start looking for the next subcharacter sequence that matches regex after the last successful subcharacter sequence. In addition, when the find method returns true, matcher can call the start () method and the end () method to get the start and end positions of the subcharacter sequence in s. When the find method returns true, matcher calls group () to return the sequence of subcharacters found by the find method that matches regex this time.

(2) public boolean matches (): matcher calls this method to determine whether the character sequence of s matches regex exactly.

(3) public boolean lookingAt (): matcher calls this method to determine whether there is a subcharacter sequence matching regex from the beginning of the character sequence of s.

(4) public boolean find (int start): matcher calls this method to determine whether there is a regex matching subcharacter sequence of the character sequence of s starting from the position specified by the parameter start. When start=0, this method has the same function as lookingAt ().

(5) public String replaceAll (String replacement): matcher calls this method to return a String object whose character sequence is obtained by replacing all the subcharacter sequences in the s character sequence that match the pattern regex with the character sequence specified by the parameter replacement (note that s itself has not changed).

(6) public String replaceFirst (String replacement): matcher calls this method to return a String object whose character sequence is obtained by replacing the first subcharacter sequence in the s character sequence that matches the pattern regex with the character sequence specified by the parameter replacement (note that s itself has not changed).

(7) public String group (): returns a String object whose character sequence is the subcharacter sequence that matches regex found by the find method in the character sequence of s.

Specific examples:

String regex= "-? [0-9] [0-9] * [.]? [0-9] *"; / / regular expression Pattern pattern=Pattern.compile (regex) for matching numbers, integers or floating-point numbers; / / initializing pattern object String s= "Local call fee: 28.39 yuan, long distance call fee: 49.15 yuan, Internet access fee: 352 yuan"; Matcher matcher=pattern.matcher (s); / / initialization matching object, which is used to retrieve sdouble sum=0 While (matcher.find ()) {String str=matcher.group (); sum+=Double.parseDouble (str); System.out.println ("matching subsequence from" + matcher.start () + "to" + matcher.end () + "); System.out.println (str);} System.out.println (" total cost: "+ sum+" yuan "); String weatherForecast [] = {" Beijing:-9 degrees to 7 degrees "," Guangzhou: 10 degrees to 21 degrees "," Harbin:-29 degrees to-7 degrees "} / / the temperature of the three places stored double averTemperture [] = new double [weatherForecast.length]; / / the average temperature of the three places stored for (int item0polii)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report