111,120
社区成员
发帖
与我相关
我的任务
分享
private void go_Click(object sender, EventArgs e)
{
string AA = "BLUE,25,1,a\r\nRED,34,1,a\r\nYELLOW,4,1,a\r\nRED,34,1,a\r\nYELLOW,4,1,a";
string[] AAlist = AA.Split(new string[]{"\r\n"},StringSplitOptions.RemoveEmptyEntries);
List<string> strlist = new List<string>();
for (int j=0;j<AAlist.Length ;j++)
{
string[] str = AAlist[j].Split(',');
for (int i = 0; i < str.Length; i++)
{
if (j ==0 )
{
strlist.Add(str[i].ToString());
}
else
{
strlist[i] += "," + str[i].ToString();
}
}
}
string laststring = "";
foreach (string s in strlist)
{
laststring += s + "\r\n";
}
this.label1.Text = laststring;
}
private static string [] Format()
{
string aa = "BLUE,25,1\r\nRED,34,1\r\nYELLOW,4,1";
//string aa = "BLUE,25,1\r\n";
//string pattern = @"^((BLUE)|(RED)|(YELLOW)),\d+,\d+\r\n$";
string pattern = @"^(((BLUE)|(RED)|(YELLOW)),\d+,\d+\r\n){2}((BLUE)|(RED)|(YELLOW)),\d+,\d+$";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
if (regex.IsMatch(aa) == false)
{
return null;//不匹配
}
string[] result = aa.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
string [] newResult = new string [3];
for (int i=0,j=0;i < result.Length;i++,j++)
{
string[] items = result[i].Split(',');
if (i == 0)
{
newResult[0] = items[0];
newResult[1] = items[1];
newResult[2] = items[2];
}
else
{
newResult[0] = newResult[0] +","+ items[0];
newResult[1] = newResult[1] + "," + items[1];
newResult[2] = newResult[2] + "," + items[2];
}
}
return newResult;
}
string aa = "BLUE,25,1\r\nRED,34,1\r\nYELLOW,4,1";
string[] result = aa.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
{
richTextBox2.Text += s + "\n";
}