java str.split(quot;cquot;),你真的完全理解了String的split方法?之二
java中在處理String字符串時,很多場合都要使用split方法
本文就全面剖析?split(String regex, int limit)的用法
先來看看API:
/ **
* @param ?regex
* ? ? ? ? the delimiting regular expression
*
* @param ?limit
* ? ? ? ? the result threshold, as described above
*
* @return ?the array of strings computed by splitting this string
* ? ? ? ? ?around matches of the given regular expression
*
* @throws ?PatternSyntaxException
* ? ? ? ? ?if the regular expression's syntax is invalid
*
* @see java.util.regex.Pattern
*
* @since 1.4
* @spec JSR-51
*/
public String[] split(String regex, int limit) {
return Pattern.compile(regex).split(this, limit);
}
經過上面一篇的內容,已經知道了第一個參數是正則表達式
這里就著重說下第二個參數的含義,就是返回值數組的最大長度
來個例子
Code:
package chapter4;
/**
* Created by MyWorld on 2016/3/28.
*/
public class StringSplitDemo {
public static void main(String[] args) {
String demoStr = "v1|v2|v3";
String[] result = demoStr.split("\\|", 2);
for (String s : result) {
System.out.println(s);
}
}
}
執行下看看是不是返回數組的長度 是不是最大是2
Output:
v1
v2|v3
沒有看懂吧
一起再來看看API
聊聊個人的理解,使用limit值,可以提高效率
就以剛才的例子,因為是split后的數組長度是3,如果limit是2,則就少匹配一次了
不少tx是不是覺得split(String regex, int limit)方法中的代碼看著有些眼熟
是的,就是正則表達式30分鐘入門系列里使用java.utilPattern
看下這個API的源碼
public String[] split(CharSequence input, int limit) {
int index = 0;
boolean matchLimited = limit > 0;
ArrayList matchList = new ArrayList();
Matcher m = matcher(input);
// Add segments before each match found
while(m.find()) {
if (!matchLimited || matchList.size() < limit - 1) {
String match =
input.subSequence
(index,
m.start()
).toString();
matchList.add(match);
index = m.end();
} else if (matchList.size() == limit - 1) { // last one
String match = input.subSequence(index, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? input.length()).toString();
matchList.add(match);
index = m.end();
}
}
// If no match was found, return this
if (index == 0)
return new String[] {input.toString()};
// Add remaining segment
if (!matchLimited || matchList.size() < limit)
matchList.add(input.subSequence(index, input.length()).toString());
// Construct result
int resultSize = matchList.size();
if (limit == 0)
while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
resultSize--;
String[] result = new String[resultSize];
return
matchList.subList(0, resultSize).toArray
(result);
}
上述源碼也印證剛才的分析。
如果確定只獲取前幾個split后的子串,使用合適的limit值會減少使用正則表達式匹配的次數,有利于提高效率
如果對多個字符串進行split操作,直接使用字符串的split(String regex)是否合適呢?
當然在功能上是沒有影響,這個地方聊聊性能吧
大家看看下面下面這兩種寫法的效率
Code:
package chapter4;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by MyWorld on 2016/3/28.
*/
public class StringSplitDemo {
public static void main(String[] args) {
List source = new ArrayList(10000);
String demoStr = "v1|v2|v3";
for (int i = 0; i < 10000; i++) {
source.add(demoStr);
} ? ? ? ?long begin = System.currentTimeMillis();
for (String str : source) {
str.split("\\|");
}
System.out.println("Cost :" + (System.currentTimeMillis() - begin));
begin = System.currentTimeMillis();
Pattern pattern = Pattern.compile("\\|");
for (String str : source) {
pattern.split(str);
}
System.out.println("Cost :" + (System.currentTimeMillis() - begin));
}
}
執行下看看結果
Output:
Cost :53
Cost :14
從上面的執行結果上看,差別不是很大
因為上面使用正則表達式也比較簡單了
使用相同正則表達式匹配多個字符串的場景,建議直接使用
java.util.regex.Pattern
而不是使用字符串的split方法
如果數據量不大,差別就不明顯,根據情況斟酌使用吧
總結
以上是生活随笔為你收集整理的java str.split(quot;cquot;),你真的完全理解了String的split方法?之二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php accesscontrolall
- 下一篇: leetcode word break