比如我有一个字符串:旅游 时尚 电影 美食
他们之间是用空格隔开的,然后我是想截取前面两个,就是:旅游 时尚
我是这么截取的,不知道为什么出错了,报错“ 索引和长度必须引用该字符串内的位置。 ”
a = a.Substring(0, 5);
他们之间是用空格隔开的,然后我是想截取前面两个,就是:旅游 时尚
我是这么截取的,不知道为什么出错了,报错“ 索引和长度必须引用该字符串内的位置。 ”
a = a.Substring(0, 5);
Match match = Regex.Match(s, @"^(\S+)\s+(\S+)");
Response.Write(match.Groups[1].Value + " " + match.Groups[2].Value);
string yourstr = "旅游 时尚 电影 美食";
int firstspace = yourstr.IndexOf(" ");
int secondspace = yourstr.Substring(firstspace + 1, yourstr.Length - firstspace - 1).IndexOf(" ");
yourstr = yourstr.Substring(0, firstspace + secondspace + 1);
Console.WriteLine(yourstr);
string str="旅游 时尚 电影 美食";
string[] arry=str.Split(' ');
StringBuilder sb=new StringBuilder ();
StringBuilder.append(arry[0]);
StringBuilder.append(" ");
StringBuilder.append(arry[1]);
Regex reg=new Regex(@"\s");
string[] strarr = reg.Split(str);
Response.Write(strarr[0] + "<br/>" + strarr[1]);
但是
1楼要加上匹配组的Length判断
3楼也要加上分割后字符串数组的Length判断