【急】.net 分割字符串|;|

asp.net 中 怎么分割
770|^|139****496|^|UNDELIV|;|770|^|1394****0141|^|UNDELIV

这个字符串,先用|;|分开,在用|^|分开,
不会的别来捣乱!
最新回答
跟悲伤比坚强。

2021-03-17 16:08:06

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[] 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, 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)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","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"} 不保留空元素
\u0002w1I+Ch%^\u0017}0string[] 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"} 不保留空元素ITPUB个人空间1K;e\u0007f\u0008f }\u0011C n
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(',')
屋顶上的小猫咪

2020-09-07 10:33:31

使用Split进行两次切分就可以了,第一次使用|;|进行切分,第二次使用|^|进行切分。

代码如下 :


纯洁的小黄瓜

2020-06-03 12:31:13

首先把分隔符替换成另一个特殊字符

char c1=(char)1;//这种特殊字符键盘是打不出来的
char c2=(char)2;//这种特殊字符键盘是打不出来的
string str ="770|^|139****496|^|UNDELIV|;|770|^|1394****0141|^|UNDELIV";
str = str.Replace("|^|",c1.ToString());
str = str.Replace("|^|",c2.ToString());

然后首先按照c1切割,切割完之后再按照c2切割
string [] arr = str.Split(c1);
for (int i = 0; i < arr.Length; i++)
{
arr[i].Split(c2);
}
寂月灭影

2022-08-29 17:56:33

string test = "770|^|139****496|^|UNDELIV|;|770|^|1394****0141|^|UNDELIV";
string[] sss = test.Split(new string[] { "|;|" }, StringSplitOptions.None);
for (int i = 0; i < sss.Length; i++)
{
sss[i].Split(new string[] { "|^|" }, StringSplitOptions.None);
}

test.Split(new string[] { "|;|","|^|" }, StringSplitOptions.None);也行吧
亡命天涯不如早点回家

2021-03-14 06:14:12

Split进行两次切分就可以了,看样子是要两组用户信息,可以循环着来