一、split(String regex)字符串分割,將字符串按照指定字符進(jìn)行分割,返回的是一個字符串?dāng)?shù)組。
public String[] split(String regex) {
return split(regex, 0);
}
原理:參數(shù)名稱是regex
表示的是以某個字符串進(jìn)行字符分割。
值得注意的是Java中使用String.split對有些特殊字符進(jìn)行分割時需要進(jìn)行轉(zhuǎn)義才能進(jìn)行分割。
例如:“| " ,”*", ". "等,否則無法分割會報錯。
實例1:根據(jù)空格切割
public static void main(String[] args) {
String k1 = "招標(biāo)人 投標(biāo)人 設(shè)計單位 施工單位";
String[] k = k1.split(" ");
System.out.println(k[0]);
System.out.println(k[1]);
System.out.println(k[2]);
System.out.println(k[3]);
}
輸出結(jié)果:
實例2:根據(jù)特殊字符進(jìn)行“.”分割
public static void main(String[] args) {
String k1 = "招標(biāo)人.投標(biāo)人.設(shè)計單位.施工單位";
String[] k = k1.split("\\.");//需要轉(zhuǎn)義
System.out.println(k[0]);
System.out.println(k[1]);
System.out.println(k[2]);
System.out.println(k[3]);
}
輸出結(jié)果:
二、split(String regex, int limit)字符串分割,返回的是字符串?dāng)?shù)組。
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
int off = 0;
int next = 0;
boolean limited = limit > 0;
ArrayList<String> list = new ArrayList<>();
while ((next = indexOf(ch, off)) != -1) {
if (!limited || list.size() < limit - 1) {
list.add(substring(off, next));
off = next + 1;
} else { // last one
//assert (list.size() == limit - 1);
list.add(substring(off, value.length));
off = value.length;
break;
}
}
// If no match was found, return this
if (off == 0)
return new String[]{this};
// Add remaining segment
if (!limited || list.size() < limit)
list.add(substring(off, value.length));
// Construct result
int resultSize = list.size();
if (limit == 0) {
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
resultSize--;
}
}
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
return Pattern.compile(regex).split(this, limit);
}
原理:split(String regex, int limit)
字符串中的regex
為非必填項,用來表示用于分割的是一個字符還是多個字符,如果忽略該選項,返回包含整個字符串的單一元素數(shù)組。Limit
可選項,該值用來限制返回中的元素個數(shù)。
注意:特殊字符需要進(jìn)行轉(zhuǎn)義。
例如:“| " ,”*", ". "等,否則無法分割會報錯。
實例1:當(dāng)regex
為空字符情況。
public static void main(String[] args) {
String k1 = "招標(biāo)人#投標(biāo)人#設(shè)計單位#施工單位";
String[] k = k1.split("",4);//需要轉(zhuǎn)義
System.out.println(k[0]);
System.out.println(k[1]);
System.out.println(k[2]);
System.out.println(k[3]);
}
運(yùn)行結(jié)果:
實例2:當(dāng)regex
為“#”號字符情況。
public static void main(String[] args) {
String k1 = "招標(biāo)人#投標(biāo)人#設(shè)計單位#施工單位";
String[] k = k1.split("#",4);//需要轉(zhuǎn)義
System.out.println(k[0]);
System.out.println(k[1]);
System.out.println(k[2]);
System.out.println(k[3]);
}
運(yùn)行結(jié)果:
實例3:當(dāng)regex
為多字符的情況
public static void main(String[] args) {
String k1 = "招標(biāo)人#投標(biāo)人#設(shè)計單位#施工單位";
String[] k = k1.split("#投標(biāo)人",4);//需要轉(zhuǎn)義
System.out.println(k[0]);
System.out.println(k[1]);
}
運(yùn)行結(jié)果:
文章來源:http://www.zghlxwxcb.cn/news/detail-502695.html
實例4:當(dāng)regex
為“#”字符且limit
限制返回2個的情況
public static void main(String[] args) {
String k1 = "招標(biāo)人#投標(biāo)人#設(shè)計單位#施工單位";
String[] k = k1.split("#",2);//需要轉(zhuǎn)義
System.out.println(k[0]);
System.out.println(k[1]);
}
運(yùn)行結(jié)果:
文章來源地址http://www.zghlxwxcb.cn/news/detail-502695.html
到了這里,關(guān)于字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!