split()用法

前端之家收集整理的这篇文章主要介绍了split()用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_2@1 @H_301_2@、用字符串分隔:
using @H_301_2@System.Text.RegularExpressions;
string @H_301_2@str @H_301_2@= @H_301_2@" @H_301_2@aaajsbbbjsccc @H_301_2@" @H_301_2@;
string @H_301_2@[]sArray @H_301_2@= @H_301_2@Regex.Split(str, @H_301_2@" @H_301_2@js @H_301_2@" @H_301_2@,RegexOptions.IgnoreCase);
foreach @H_301_2@( string @H_301_2@i in @H_301_2@sArray)Response.Write(i.ToString() @H_301_2@+ @H_301_2@ @H_301_2@" @H_301_2@<br> @H_301_2@" @H_301_2@);
输出结果:
aaa
bbb
ccc
@H_301_2@2 @H_301_2@、用多个字符来分隔:
string @H_301_2@str @H_301_2@= @H_301_2@" @H_301_2@aaajbbbscccjdddseee @H_301_2@" @H_301_2@;
string @H_301_2@[]sArray @H_301_2@= @H_301_2@str.Split( new @H_301_2@ char @H_301_2@[ @H_301_2@2 @H_301_2@] @H_301_2@{@H_301_2@'@H_301_2@j@H_301_2@'@H_301_2@,@H_301_2@'@H_301_2@s@H_301_2@'@H_301_2@} @H_301_2@);
foreach @H_301_2@( string @H_301_2@i in @H_301_2@sArray)Response.Write(i.ToString() @H_301_2@+ @H_301_2@ @H_301_2@" @H_301_2@<br> @H_301_2@" @H_301_2@);
输出结果:
aaa
bbb
ccc
ddd
eee
@H_301_2@3 @H_301_2@、用单个字符来分隔:
string @H_301_2@str @H_301_2@= @H_301_2@" @H_301_2@aaajbbbjccc @H_301_2@" @H_301_2@;
string @H_301_2@[]sArray @H_301_2@= @H_301_2@str.Split( @H_301_2@' @H_301_2@j @H_301_2@' @H_301_2@);
foreach @H_301_2@( string @H_301_2@i in @H_301_2@sArray)Response.Write(i.ToString() @H_301_2@+ @H_301_2@ @H_301_2@" @H_301_2@<br> @H_301_2@" @H_301_2@);
输出结果:
aaa
bbb
ccc

@H_301_2@

@H_301_2@ @H_301_2@
////////////////////////////////////////////////
string[] arr = str.Split("o");

这是一个具有语法错误的语句,Split 的 separator 参数应该是 char[] 或 string[],不应是字符串。正确的示例:

string str = "technology";
char[] separator = { 'o' };
string[] arr = str.Split(separator);
////////////////////////////////////////////////////

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,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) ITPUB个人空间,n:H!C0M/S3U\u0002P
程序代码
string[] split = words.Split(new Char[] { ','.' },2);//返回:{"1","2.3,4"}
string[] split = words.Split(new Char[] { ',6);//返回:{"1","4"}
3. public string[] Split(char[] separator,StringSplitOptions options)
程序代码
string[] split = words.Split(new Char[] { ',StringSplitOptions.RemoveEmptyEntries);//返回:{"1","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',StringSplitOptions.None);//返回:{"1","4"} 保留空元素
4. public string[] Split(string[] separator,StringSplitOptions options)
程序代码
string[] split = words.Split(new string[] { ",","." },"4"} 不保留空元素
\u0002w1I+Ch%^\u0017}0string[] split = words.Split(new string[] { ","4"} 保留空元素
5. public string[] Split(char[] separator,2,4"} 不保留空元素 ITPUB个人空间1K;e\u0007f\u0008f }\u0011C n
string[] split = words.Split(new Char[] { ',6,"4"} 保留空元素
6. public string[] Split(string[] separator,4"} 不保留空元素
string[] split = words.Split(new string[] { ","4"} 保留空元素
需要注意的是没有重载函数public string[] Split(string[] separator),所以我们不能像VB.NET那样使用words.Split(","),而只能使用words.Split(',')

猜你在找的VB相关文章