Three methods of Java string segmentation (summary)

Recently, I encountered a small problem in the project. A string is divided into an array, similar to string STR = "AAA, BBB, CCC"; Then take "," as the delimiter and divide it into an array. What method is used to realize it?

The first method:

You may suddenly think of using split () method. It is the most convenient to use split () method, but its efficiency is relatively low

The second method:

Use the more efficient stringtokenizer class to segment strings. Stringtokenizer class is a tool class provided in JDK to process string segmentation substrings. Its constructor is as follows:

STR is the string to be split and delim is the split symbol. After a stringtokenizer object is generated, the next split string can be obtained through its nexttoken() method, and then you can know whether there are more substrings to be processed through hasmoretokens() method. This method is more efficient than the first one.

The third method:

The two methods of using string, indexof () and substring (), substring () use time for space technology, so its execution efficiency will be relatively fast. As long as the memory overflow problem is handled well, it can be used boldly. The indexof() function is a very fast method,

The prototype is as follows:

Public int indexof (int CH) returns the position of the specified character in the string object. As follows:

give an example:

The above three methods (summary) of Java string segmentation are all the contents shared by Xiaobian. I hope to give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>