C#我如何拆分字符串?

时间:2017-07-07 05:47:35

标签: c# string split

有一个文件包含一些字符串,我将编写一个C#表单项目。我需要将它们拆分为“:”,并且在拆分标签后会显示拆分的字符串。

这是我应该拆分的文字

1096:001:161208:092218:1935:001:H:101:0006:                :00+00000+000000001
1096:001:161208:092218:1935:002:w:100:0006:                :00:00000:00000000R
1096:001:161208:092218:1935:003:S:101:0508:   2416100000006+00010010*000000140
1096:001:161208:092221:1935:004:S:101:0501:   8691397181056+00010010*000000250
1096:001:161208:092228:1935:005:l:100:0000:LCS SUCCESS     :00000000+000000000
1096:001:161208:092231:1935:006:T:110:0006:                :01+00001+000002000
1096:001:161208:092231:1935:007:T:114:0006:                :01+00000-000001610
1096:001:161208:092231:1935:008:V:111:0006:          %1    :00+00001+000000248
1096:001:161208:092231:1935:009:V:110:0006:          %1    :00+00001+000000002
1096:001:161208:092231:1935:010:V:121:0006:          %8    :00+00001+000000130
1096:001:161208:092231:1935:011:V:120:0006:          %8    :00+00001+000000010
1096:001:161208:092231:1935:012:w:100:0006:                :00:00000:00000000R
1096:001:161208:092231:1935:013:q:100:0000:                :000000000000000000
1096:001:161208:092231:1935:014:F:100:0006:                :00+00002+000000390
FIS :001:161208:092231:1935:015: :100:0006:TN 13091080     :00+01178+000000390

软件将从txt文件中导入这些数据,之后它将拆分所有“:”,并将写入表格中的标签。

应该是这样的:

Label1=1096 Label2=001 Label3=161208 Label4=092218 Label5=1935 Label6=001
Label7=H Label8=101 Label9=0006 Label10=00 Label11=00000 Label12=000000001

此外,它应该在Label13

中的“:”之间找到%1%1%8%8的空格

1 个答案:

答案 0 :(得分:1)

<强> String.Split()

虽然我有这种感觉你甚至没有尝试谷歌你的问题,并且不知道如何编码任何东西(String.Split非常简单),这里有一些代码可以让你开始:

            string source = @"
1096:001:161208:092218:1935:001:H:101:0006:                :00+00000+000000001
1096:001:161208:092218:1935:002:w:100:0006:                :00:00000:00000000R
1096:001:161208:092218:1935:003:S:101:0508:   2416100000006+00010010*000000140
1096:001:161208:092221:1935:004:S:101:0501:   8691397181056+00010010*000000250
1096:001:161208:092228:1935:005:l:100:0000:LCS SUCCESS     :00000000+000000000
1096:001:161208:092231:1935:006:T:110:0006:                :01+00001+000002000
1096:001:161208:092231:1935:007:T:114:0006:                :01+00000-000001610
1096:001:161208:092231:1935:008:V:111:0006:          %1    :00+00001+000000248
1096:001:161208:092231:1935:009:V:110:0006:          %1    :00+00001+000000002
1096:001:161208:092231:1935:010:V:121:0006:          %8    :00+00001+000000130
1096:001:161208:092231:1935:011:V:120:0006:          %8    :00+00001+000000010
1096:001:161208:092231:1935:012:w:100:0006:                :00:00000:00000000R
1096:001:161208:092231:1935:013:q:100:0000:                :000000000000000000
1096:001:161208:092231:1935:014:F:100:0006:                :00+00002+000000390
FIS :001:161208:092231:1935:015: :100:0006:TN 13091080     :00+01178+000000390            
            ";
            string[] lines = source.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            foreach(string line in lines) {
                string[] values = line.Split(new char[] {':'}, StringSplitOptions.RemoveEmptyEntries);
                int counter = 1;
                foreach(string value in values) {
                    Console.Write("Label" + counter + ": " + value + "   ");
                    counter++;
                }
                Console.WriteLine("-");
            }

            Console.ReadLine();

启动新的控制台项目并将其粘贴到Main()。其余的由你决定。