C#中split的方法和用法
- 提问者网友:却不属于对方
- 2021-05-02 14:05
- 五星知识达人网友:佘樂
- 2021-05-02 15:29
第一种方法:
string s=abcdeabcdeabcde;
string[] sArray=s.Split('c') ;
foreach(string i in sArray)
Console.WriteLine(i.ToString());
输出下面的结果:
ab
deab
deab
de
第二种方法:
我们看到了结果是以一个指定的字符进行的分割。使用另一种构造方法对多个字符进行分割:
string s="abcdeabcdeabcde";
string[] sArray1=s.Split(new char[3]{'c','d','e'}) ;
foreach(string i in sArray1)
Console.WriteLine(i.ToString());
可以输出下面的结果:
ab
ab
ab
第三种方法:
除了以上的这两种方法以外,第三种方法是使用正则表达式。新建一个控制台项目。然后先添加 using System.Text.RegularExpressions;
System.Text.RegularExpressions
string content=agcsmallmacsmallgggsmallytx;
string[]resultString=Regex.Split(content,small,RegexOptions.IgnoreCase)
foreach(string i in resultString)
Console.WriteLine(i.ToString());
输出下面的结果:
agc
mac
ggg
ytx
第四种方法:
string str1=我*****是*****一*****个*****教*****师;
string[] str2;
str1=str1.Replace(*****,*) ;
str2=str1.Split(*) ;
foreach(string i in str2)
Console.WriteLine(i.ToString());
第五种方法:
string str1=我**是*****一*****个*****教*****师;
我希望显示的结果为:我是一个教师。
我如果采用上面的第四种方法来做就会产生下面的错误:我 是一个教师。中间有空格输出,所以输出结果并不是希望的结果,这就又回到了正则表达式了,这时可以采用下面的第五种方法:
string str1=我**是*****一*****个*****教*****师;
string[] str2 = System.Text.RegularExpressions.Regex.Split(str1,@[*]+);
foreach(string i in str2)
Console.WriteLine(i.ToString());
- 1楼网友:十鸦
- 2021-05-02 16:01
分割字符串函数split
/// <summary> /// 字符串分割符 /// </summary> public const string SEPARATORSTRING = "@#$@#$";
/// <summary> /// 取得字符的第几个字符,通过分割符分割的字符串,pos以1开始 /// </summary> /// <param name="str">字符串</param> /// <param name="separator">分割符</param> /// <param name="pos">第几个</param> /// <returns>返回第几个字符串</returns> /// <example>string str = Split("rrrrddrew@@tqwewerewddccc", "@@", 1);</example> public static string Split(string str, string separator, int pos) { int Len = str.Length; int Count = str.Length - str.Replace(separator, "").Length; int BitCount = 0; string returnValue = ""; string strValue = ""; string InSeparator = ""; if (Count == 0) { return str; } InSeparator = str.Substring(str.Length - separator.Length, separator.Length); if (InSeparator != separator) { str += separator; }
for (int i = 0; i < Count; i++) { BitCount = str.IndexOf(separator, 0); strValue = str.Substring(0, BitCount); str = str.Replace(strValue + separator, ""); if (i == pos - 1) { returnValue = strValue; break; } } return returnValue; } /// <summary> /// 取得字符的第几个字符,通过分割符分割的字符串,pos以1开始,以默认的字符串分割 /// </summary> /// <param name="str">字符串</param> /// <param name="pos">第几个</param> /// <returns>返回第几个字符串</returns> /// <example>string str = Split("rrrrddrew@@tqwewerewddccc",1);</example> public static string Split(string str, int pos) { int Len = str.Length; int Count = str.Length - str.Replace(SEPARATORSTRING, "").Length; int BitCount = 0; string returnValue = ""; string strValue = ""; string InSeparator = ""; if (Count == 0) { return str; } InSeparator = str.Substring(str.Length - SEPARATORSTRING.Length, SEPARATORSTRING.Length); if (InSeparator != SEPARATORSTRING) { str += SEPARATORSTRING; }
for (int i = 0; i < Count; i++) { BitCount = str.IndexOf(SEPARATORSTRING, 0); strValue = str.Substring(0, BitCount); str = str.Replace(strValue + SEPARATORSTRING, ""); if (i == pos - 1) { returnValue = strValue; break; } } return returnValue; } /// <summary> /// 取得分割后的字符串数据,数组以0开始 /// </summary> /// <param name="str">字符串</param> /// <param name="separator">分割符</param> /// <returns>返回分割后的字符串数组</returns> /// <example>string str=Split("rrrrddrew@@tqwewerewddccc@@", "@@")[1].ToString()</example> public static string[] Split(string str, string separator) { int Len = str.Length; int Count = 0; int BitCount = 0; string strValue = ""; string InSeparator = ""; InSeparator = str.Substring(str.Length - separator.Length, separator.Length); if (InSeparator != separator) { str += separator; } Count = (str.Length - str.Replace(separator, "").Length) / separator.Length; string[] returnValue = new string[Count]; for (int i = 0; i < Count; i++) { BitCount = str.IndexOf(separator, 0); strValue = str.Substring(0, BitCount); str = str.Replace(strValue + separator, ""); //MessageBox.Show(BitCount.ToString() + ";" + strValue.ToString() + ";" + str); returnValue[i] = strValue;
} return returnValue; } /// <summary> /// 取得分割后的字符串数据,数组以0开始,以默认的字符串分割 /// </summary> /// <param name="str">字符串</param> /// <returns>返回分割后的字符串数组</returns> /// <example>string str=Split("rrrrddrew@@tqwewerewddccc@@")[1].ToString()</example> public static string[] Split(string str) { int Len = str.Length; int Count = 0; int BitCount = 0; string strValue = ""; string InSeparator = ""; InSeparator = str.Substring(str.Length - SEPARATORSTRING.Length, SEPARATORSTRING.Length); if (InSeparator != SEPARATORSTRING) { str += SEPARATORSTRING; } Count = (str.Length - str.Replace(SEPARATORSTRING, "").Length) / SEPARATORSTRING.Length; string[] returnValue = new string[Count]; for (int i = 0; i < Count; i++) { BitCount = str.IndexOf(SEPARATORSTRING, 0); strValue = str.Substring(0, BitCount); str = str.Replace(strValue + SEPARATORSTRING, ""); //MessageBox.Show(BitCount.ToString() + ";" + strValue.ToString() + ";" + str); returnValue[i] = strValue;
} return returnValue; }