這是你所了解的Split 函式嗎

2021-06-21 02:42:07 字數 3786 閱讀 1500

在做bs專案時,經常涉及到前後臺傳輸資料,當然,字串的拼接會經常用到,而對於字串的分割,我們往往使用到string.split()函式。但是你真正的了解他嗎?

string.split給我們提供了非常靈活的使用方式, 但是如果使用不當, 會造成錯誤。下面就來看看——這是不是你所了解的split()函式。

一、簡介

先看一段**:

string s = "a|b|:|c:d";

string ss = s.split("|:|".tochararray());

//ss[0]: a

//ss[1]: b

//ss[2]:

//ss[3]:

//ss[4]: c

//ss[5]: d

顯而易見,本意是使用」|:|」將字串分開的, 目標是為了得到陣列: 「a|b」 和 「c:d」, 但是沒有直接找到split(string)的過載, 就用了tochararray(), 很顯然, 得到的結果是錯誤的.

正確的用法為:

string ss1 = s.split(new , stringsplitoptions.none);

//ss1[0]: a|b

//ss1[1]: c:d

後乙個引數, 是標誌是否自動remove empty的資料. 例如: "a|b|:|c:d|:|" 做分割時, 會返回三個陣列, 該陣列的最後乙個元素是空"", 如果我們想過濾掉這些空的元素, 可以使用引數: stringsplitoptions.removeemptyentries 即:

string ss1 = s.split(new , stringsplitoptions.removeemptyentries); 

對比一下,看個例子:

s = "a|b|:|c:d|:|";

string ss1 = s.split(new , stringsplitoptions.removeemptyentries);

//ss1[0]: "a|b"

//ss1[1]: "c:d"

ss1 = s.split(new , stringsplitoptions.none);

//ss1[0]: "a|b"

//ss1[1]: "c:d"

//ss1[2]: ""

二、補充

下面我們看下string.split 方法有6個過載函式:

1) public string split(params char separator)

2) public string split(char separator, int count)

3) public string split(char separator, stringsplitoptions options)

4) public string split(string separator, stringsplitoptions options)

5) public string split(char separator, int count, stringsplitoptions options)

6) public string split(string separator, int count, stringsplitoptions options)

直觀起見,我們通過一些例項來說明下怎麼使用(以下string words = "1,2.3,,4";):

1. public string split(params char separator)

程式**:

string split = words.split(new char );//返回:

string split = words.split(new char );//返回:

2. public string split(char separator, int count)

程式**:

string split = words.split(new char , 2);//返回:

string split = words.split(new char , 6);//返回:

3. public string split(char separator, stringsplitoptions options)

程式**

string split = words.split(new char , stringsplitoptions.removeemptyentries);//返回: 不保留空元素

string split = words.split(new char , stringsplitoptions.none);//返回: 保留空元素

4. public string split(string separator, stringsplitoptions options)

程式**

string split = words.split(new string , stringsplitoptions.removeemptyentries);//返回: 不保留空元素

string split = words.split(new string , stringsplitoptions.none);//返回: 保留空元素

5. public string split(char separator, int count, stringsplitoptions options)

程式**

string split = words.split(new char , 2, stringsplitoptions.removeemptyentries);//返回: 不保留空元素

string split = words.split(new char , 6, stringsplitoptions.none);//返回: 保留空元素

6. public string split(string separator, int count, stringsplitoptions options)

程式**

string split = words.split(new string , 2, stringsplitoptions.removeemptyentries);//返回: 不保留空元素

string split = words.split(new string , 6, stringsplitoptions.none);//返回: 保留空元素

個人理解:split 就是和string.join(下篇部落格介紹)相反的乙個函式,它的作用是把乙個字串分隔成含有多個欄位的陣列。

我現在只對上面的一種方法進行分析:

string split = words.split(new char , 6);//返回:
為什麼返回的有了個空的「」 呢,自己當時也是不理解。其實它就是以" , " 和「 . " 兩個分隔符來拆分字串 1,2.3,,4的,當找到 3 後面的第乙個「, 」 時候說明此處要分開了。但是它後面又是乙個分隔符 「, 」,怎麼辦呢?這是就只能當成 " " 空來處理了。你可以變相的把1,2.3,,4連續的兩個" , "間認為有個空格符「 」也可以。

——文章結束,到此為止,這是不是你所了解的split()函式呢?

不為你所了解的諾基亞

公司型別public omx nok1v nyse nok stockholmsborsen noki frankfurter wertpapierborse noa3 成立時間芬蘭諾基亞 1865年 總部地點芬蘭埃斯波 重要人物fredrik idestam,founder jorma ollil...

你所了解的設計模式

定義 示例 singletonpattern 單例模式的特點 1 私有的構造方法 2 私有的靜態的變數 3 自定義的例項化的方法 public class singleton 要素2 靜態私有成員變數 private static singleton objinstance 要素3 public s...

簡單了解split 函式的性質

當分割的字元在字串中間時,分割字元前面為一部分,後面為一部分。如 st abccd print st.split b 輸出為 a ccd 當分隔符在字串最前面或最後麵時,分割後後面的明顯為一部分,但是最前面會有乙個空白位置。如 st abccd print st.split a print st.s...