java關(guān)于String.split("|")的使用方式
String.split("|")的使用
我們先來寫一段代碼測試一下
public class TestSplit {
public static void main(String[] a){
String test = "中文|英文";
print(test.split("|"));
print(test.split(""));
print(test.split("\\|"));
}
public static void print(String[] a){
System.out.println("============================");
for(String i:a){
System.out.println(i);
}
System.out.println("============================\n");
}
}你知道結(jié)果是什么嗎?
如下:
============================
中
文
|
英
文
========================================================
中
文
|
英
文
========================================================
中文
英文
============================
所以我們從上面可以知道:“|”和“”的效果是一樣的,如果你要得到正確的結(jié)果你必須這樣“\|”,雙引號里面的是一個(gè)正則表達(dá)式。

String.split() 特殊字符處理
- jdk 1.8
split函數(shù)
注意,split函數(shù)的參數(shù)是正則表達(dá)式。split函數(shù)的定義為:
/**
* Splits this string around matches of the given <a
* href="../util/regex/Pattern.html#sum" rel="external nofollow" >regular expression</a>.
*
* <p> This method works as if by invoking the two-argument {@link
* #split(String, int) split} method with the given expression and a limit
* argument of zero. Trailing empty strings are therefore not included in
* the resulting array.
*
* <p> The string {@code "boo:and:foo"}, for example, yields the following
* results with these expressions:
*
* <blockquote><table cellpadding=1 cellspacing=0 summary="Split examples showing regex and result">
* <tr>
* <th>Regex</th>
* <th>Result</th>
* </tr>
* <tr><td align=center>:</td>
* <td>{@code { "boo", "and", "foo" }}</td></tr>
* <tr><td align=center>o</td>
* <td>{@code { "b", "", ":and:f" }}</td></tr>
* </table></blockquote>
*
*
* @param regex
* the delimiting regular expression
*
* @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) { ... }
特殊符號的處理
split函數(shù)的參數(shù)是正則表達(dá)式,則正則表達(dá)式的特殊符號作為分隔符時(shí),就需要特殊處理。
比如,.在正則表達(dá)式中是通配符,匹配除換行符(\n、\r)之外的任何單個(gè)字符。
對特殊符號的處理方法有兩種:
- 轉(zhuǎn)義。比如,\.
- 放到中括號里。比如,[.]
示例
String[] s1 = "a.b.c".split("\\.");
System.out.println(Arrays.asList(s1)); //[a, b, c]
String[] s2 = "a.b.c".split("[.]");
System.out.println(Arrays.asList(s2)); //[a, b, c]以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java?Date和SimpleDateFormat時(shí)間類詳解
這篇文章主要介紹了java?Date和SimpleDateFormat時(shí)間類詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
myeclipse導(dǎo)出可運(yùn)行jar包簡介
這篇文章主要介紹了myeclipse導(dǎo)出可運(yùn)行jar包簡介,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Spring Boot動態(tài)加載Jar包與動態(tài)配置實(shí)現(xiàn)
隨著項(xiàng)目的不斷演進(jìn)和業(yè)務(wù)需求的增長,很多場景下需要實(shí)現(xiàn)系統(tǒng)的動態(tài)性和靈活性,本文主要介紹了Spring Boot動態(tài)加載Jar包與動態(tài)配置實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Java ArrayList.toArray(T[]) 方法的參數(shù)類型是 T 而不是 E的原因分析
這篇文章主要介紹了Java ArrayList.toArray(T[]) 方法的參數(shù)類型是 T 而不是 E的原因分析的相關(guān)資料,需要的朋友可以參考下2016-04-04
SpringBoot隨機(jī)端口啟動的實(shí)現(xiàn)
本文主要介紹了SpringBoot隨機(jī)端口啟動的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
java Class.getSimpleName() 詳解及用法
這篇文章主要介紹了java Class.getSimpleName() 詳解及用法的相關(guān)資料,需要的朋友可以參考下2017-02-02
SpringBoot 整合Redisson重寫cacheName支持多參數(shù)的案例代碼
這篇文章主要介紹了SpringBoot 整合Redisson重寫cacheName支持多參數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01

