日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java String.split() 特殊字符处理

發(fā)布時間:2024/9/19 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java String.split() 特殊字符处理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

  • 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">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á)式的特殊符號作為分隔符時,就需要特殊處理。

比如,.在正則表達(dá)式中是通配符,匹配除換行符(\n、\r)之外的任何單個字符。

對特殊符號的處理方法有兩種:

  • 轉(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]

參考

https://www.runoob.com/regexp/regexp-metachar.html

總結(jié)

以上是生活随笔為你收集整理的Java String.split() 特殊字符处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。