Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C中的字符串中提取多个子字符串?_C#_String_Substring - Fatal编程技术网

C# 从C中的字符串中提取多个子字符串?

C# 从C中的字符串中提取多个子字符串?,c#,string,substring,C#,String,Substring,我有固定长度为14的字符串。。。字符串由0到9的数字组成。。。现在我必须从中提取3个不同的子字符串,其中第一个子字符串将有前2个字符,然后第二个子字符串将有下6个字符,最后一个子字符串将有最后6个字符。。例如 string = "12345678901234"; substr1 =" 12"; substr2 = "345678"; substr3 = "901234"; 像这样。。。有人帮忙吗 回答很差,效率很低,代码行比需要的多。我也是新手。我不太了解所有的方法,比如子字符串。我只是想

我有固定长度为14的字符串。。。字符串由0到9的数字组成。。。现在我必须从中提取3个不同的子字符串,其中第一个子字符串将有前2个字符,然后第二个子字符串将有下6个字符,最后一个子字符串将有最后6个字符。。例如

string =  "12345678901234";

substr1 =" 12";
substr2 = "345678";
substr3 = "901234";
像这样。。。有人帮忙吗


回答很差,效率很低,代码行比需要的多。我也是新手。我不太了解所有的方法,比如子字符串。我只是想帮你。谢谢,这就是我要找的。。。我可以使用子字符串,但我不想要它。。。很抱歉没有告诉你这个问题。。我非常愿意接受任何批评,所以我不介意。。。再次感谢@dotnet\u nobWell,结果出人意料地好@你为什么不在问题中提到这个要求?在不使用string.Substring的情况下,这实际上是一个相当合理的答案。除非是学术性的练习,否则几乎没有理由不使用string.Substring。@tomfanning no这不是学术性的:D我只是想要一些不同的东西,因为我当前的项目不是很有挑战性,所以我正在尝试不同的东西。。
string x  =  "12345678901234";
MessageBox.Show(x.Substring(0, 2)); // sub1
MessageBox.Show(x.Substring(2, 6)); // sub2
MessageBox.Show(x.Substring(8, 6)); // sub3
string str = "12345678901234";

string str1 = string.Empty;
string str2 = string.Empty;
string str3 = string.Empty;
for (int i = 0; i < str.Length; i++)
{
    if (i < 2)
        str1 += str[i];
    else if (i > 1 && i < 8)
        str2 += str[i];
    else
        str3 += str[i];
}