[C#]中String、String[]、List<string>相互转换及string成员substring、split简单使用

一、相互转换

1. String > String[]

String s = "a,b,c,d,e";
String[] sArray = s.Split(',');

2. String[] > String

string[] sArray = {"a", "b", "c", "d","e"};
string s = String.Join(",", sArray);

3.String[] > List<String>

string[] sArray = {"a", "b", "c", "d","e"};
List<String> list = new List<string>(sArray);

4.List<String> > String[]

List<String> list = new List<string>();
list.Add("a");            
list.Add("b");            
list.Add("c");            
list.Add("d");    
list.Add("e");     
string[] sArray = list.ToArray();

备注:String和List<String>之间的转换可以使用String[]来中转完成

二、String常用操作成员

1、常用

 

2、Substring介绍

    Substring(param1,param2)


param1参数是起始位置,这是原字符串的初始位置:注意:是从该位置开始取,但是不包括这个点,
param2参数是结束位置,可以省略,省略的话就是从开始位置一直取到最后,如果不省略,就是取得指定的长度;

Eg:

string a = “abcde.12345”:   a.Substring(3,2)   结果为:de

string a = “abcde.12345”:   a.Substring(3,2)   结果为:de.12345

string a = “abcde.12345”:   a.Substring(a.LastIndexOf('.')+1)   结果为:12345

 

3、Split分隔符介绍

1、用字符串分隔:
using System.Text.RegularExpressions;
string str="aaajsbbbjsccc";
string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);
foreach (string i in sArray) Response.Write(i.ToString() + "<br>");
输出结果:
aaa
bbb
ccc
2、用多个字符来分隔:
string str="aaajbbbscccjdddseee";

string[] sArray=str.Split(new char[2] {'j','s'});
foreach(string i in sArray) Response.Write(i.ToString() + "<br>");
输出结果:
aaa
bbb
ccc
ddd
eee
3、用单个字符来分隔:
string str="aaajbbbjccc";
string[] sArray=str.Split('j');
foreach(string i in sArray) Response.Write(i.ToString() + "<br>");
输出结果:
aaa
bbb
ccc

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[] { ',' });//返回:{"1","2.3","","4"}
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
2. public string[] Split(char[] separator, int count)

程序代码

string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
3. public string[] Split(char[] separator, StringSplitOptions options)

程序代码

string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
4. public string[] Split(string[] separator, StringSplitOptions options)

程序代码

string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5. public string[] Split(char[] separator, int count, StringSplitOptions options)

程序代码

string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6. public string[] Split(string[] separator, int count, StringSplitOptions options)

程序代码

string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

需要注意的是没有重载函数public string[] Split(string[] separator),所以我们不能像VB.NET那样使用words.Split(","),而只能使用words.Split(',')

正则匹配拆分字符串提供了5个重载函数:

using System.Text.RegularExpressions;

1.Regex.Split(String,String)

string str = "aaatsbbtscctsdd";
stirng []strArray = Regex.Split(str,"ts");  //正则匹配切割(result: "aaa" "bb" "cc" "dd");

2.Regex.Split(String,String)

string str = "aaatsbbtscctsdd";
stirng []strArray = Regex.Split(str,"ts",RegexOptions.IgnoreCase);  //正则匹配切割(result: "aaa" "bb" "cc" "dd");

三、应用

1.单字符拆分

string str = "aaatbbscctdd";

string []strArray = str.Split(new char[]{'t'}); 或string []strArray = str.Split('t'); //单字符切割(result : "aaa"  "bbscc"  "dd")

2.多字符拆分

string str = "aaatbbscctdd";

string []strArray = str.Split(new char[]{'t','s'}); //多字节切割(result : "aaa" "bb" "cc" "dd")

3.字符串拆分

using System.Text.RegularExpressions;

string str = "aaatsbbtscctsdd";

stirng []strArray = Regex.Split(str,"ts",RegexOptions.IgnoreCase);  //正则匹配切割且(result: "aaa" "bb" "cc" "dd");

或者

string str = "aaatsbbtscctsdd";

string []strArray = str.Split(new string[]{"ts"});  //字符串切割(result : "aaa" "bb" "cc" "dd")

注意:

1.String.Split方法没有重载String.Split(stirng [])方法,也就是说str.Split("t")或者str.Split(new string[]{"ts","dd"})都是错误的。

2.除了切割单字符可用String.Split('t')外,其它的都必须用String.Split(new char[]{})这样的格式,否则编译会出错。



posted @ 2023-04-24 22:23  shenxiang_  阅读(4805)  评论(0编辑  收藏  举报