【发布时间】:2017-11-21 16:00:47
【问题描述】:
我有一个字符串,我将其拆分为 2 个字符串,如下所示:
string a="Hello World here i am ";
if(a.Length > 10)
{
string[] result = a.Split(' '); // Divides string into 2 where there is a Space this is type of array
string C = result[0]; // This takes the 1st value of that array
string D = result[1];);//This takes the value of that array
Console.WriteLine(C);
Console.WriteLine(D);
}
这是一个用于测试真实的控制台行示例,我将需要 2 个字符串将 cmets 放入注释框中的 2 行中。
所以这个字符串 a 可以是任何东西,问题是我只有 2 行。我想将它从这个字符串中间的空格中拆分出来,例如一个计算字符数的代码,例如 a.Length > 10 中的字符数,然后找到这个字符串中间的空格例如它是Hello World here I am,在这里它应该在一个字符串中看到Hello world,在这里我进入另一个字符串有什么帮助吗?我试着看很多这样的例子:
string s = "there is a cat";
//
// Split string on spaces.
// ... This will separate all the words.
//
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
这会将它们分成几行,并且不会真正返回 2 个字符串,我只想要 2 个字符串。提前致谢
【问题讨论】:
-
s.Length/2为您提供中间字符的索引。现在使用 SubString 方法获取中间字符前后的字符串 -
您需要准确地指定要在规则上拆分字符串的空间以确定这一点。那么你想要“there”和“is a cat”还是“there is”和“a cat”或者“there is a”和“cat”?
-
@juharr 正如我在问题中所说,它是一个评论框,它可以是任何用户可以输入的任何内容从中间
-
s.Length/2 会试一试,但不会断字\
-
@HaseebAhmad 那么中间空间?那是导致两个字符串尽可能接近相同长度的空间还是 n/2 空间,其中 n 是空格数?那么“a a a a a reallylongword”会分为“a a a”和“a a reallongword”还是“a a a a a”和“reallylongword”?
标签: c# asp.net arrays string split