[英]How do I extract a substring from a string until the second space is encountered?
我有一个这样的字符串:
"o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467"
如何只提取"o1 1232.5467"
?
要提取的字符数并不总是相同的。 因此,我只想提取直到遇到第二个空格。
一个简单的方法如下:
string[] tokens = str.Split(' ');
string retVal = tokens[0] + " " + tokens[1];
只需使用String.IndexOf两次,如:
string str = "My Test String";
int index = str.IndexOf(' ');
index = str.IndexOf(' ', index + 1);
string result = str.Substring(0, index);
获得第一个空间的位置:
int space1 = theString.IndexOf(' ');
之后的下一个空间的位置:
int space2 = theString.IndexOf(' ', space1 + 1);
获取字符串的一部分到第二个空格:
string firstPart = theString.Substring(0, space2);
上面的代码变成了一个单行代码:
string firstPart = theString.Substring(0, theString.IndexOf(' ', theString.IndexOf(' ') + 1));
s.Substring(0, s.IndexOf(" ", s.IndexOf(" ") + 1))
使用正则表达式:。
Match m = Regex.Match(text, @"(.+? .+?) ");
if (m.Success) {
do_something_with(m.Groups[1].Value);
}
像这样的东西:
int i = str.IndexOf(' ');
i = str.IndexOf(' ', i + 1);
return str.Substring(i);
string[] parts = myString.Split(" ");
string whatIWant = parts[0] + " "+ parts[1];
string testString = "o1 1232.5467 1232.5467.........";
string secondItem = testString.Split(new char[]{' '}, 3)[1];
:P
只是一个注释,我认为这里的大多数算法都不会检查你是否有2个或更多的空格,所以它可能会得到一个空格作为第二个单词。
我不知道它是不是最好的方式,但我有一点乐趣林清它:P(好的是它让你选择你想要的空格/单词的数量)
var text = "a sdasdf ad a";
int numSpaces = 2;
var result = text.TakeWhile(c =>
{
if (c==' ')
numSpaces--;
if (numSpaces <= 0)
return false;
return true;
});
text = new string(result.ToArray());
我也得到了@ ho的答案并将它变成了一个循环,所以你可以再次使用它来获得任意数量的单词:P
string str = "My Test String hello world";
int numberOfSpaces = 3;
int index = str.IndexOf(' ');
while (--numberOfSpaces>0)
{
index = str.IndexOf(' ', index + 1);
}
string result = str.Substring(0, index);
像其他人所说的那样有更短的方法,但你也可以检查每个角色,直到你自己遇到第二个空格,然后返回相应的子串。
static string Extract(string str)
{
bool end = false;
int length = 0 ;
foreach (char c in str)
{
if (c == ' ' && end == false)
{
end = true;
}
else if (c == ' ' && end == true)
{
break;
}
length++;
}
return str.Substring(0, length);
}
你可以先尝试找到indexOf“o1”。 然后提取它。 在此之后,使用字符“1232.5467”拆分字符串:
string test = "o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467";
string header = test.Substring(test.IndexOf("o1 "), "o1 ".Length);
test = test.Substring("o1 ".Length, test.Length - "o1 ".Length);
string[] content = test.Split(' ');
我会推荐一个正则表达式,因为它处理你可能没有考虑过的案例。
var input = "o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467";
var regex = new Regex(@"^(.*? .*?) ");
var match = regex.Match(input);
if (match.Success)
{
Console.WriteLine(string.Format("'{0}'", match.Groups[1].Value));
}
我正在为我自己的代码考虑这个问题,尽管我可能最终会使用更简单/更快的东西,这是另一个与@Francisco添加的类似的Linq解决方案。
我只是喜欢它,因为它读取的内容最像您实际想做的事情:“在生成的子字符串少于2个空格时使用字符。”
string input = "o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467";
var substring = input.TakeWhile((c0, index) =>
input.Substring(0, index + 1).Count(c => c == ' ') < 2);
string result = new String(substring.ToArray());
我在这篇文章中为一些解决方案做了一些基准测试并得到了这些结果(如果性能很重要):
最快的方法(@Hans Olsson 的回答):
string str = "My Test String";
int index = str.IndexOf(' ');
index = str.IndexOf(' ', index + 1);
string result = str.Substring(0, index);
@Guffa 为这个解决方案添加了一行代码(并一点一点地解释了发生了什么),这是我个人更喜欢的。
检查的方法:
public string Method1(string str)
{
string[] tokens = str.Split(' ');
return tokens[0] + " " + tokens[1];
}
public string Method2(string str)
{
int index = str.IndexOf(' ');
index = str.IndexOf(' ', index + 1);
return str.Substring(0, index);
}
public string Method3(string str)
{
Match m = Regex.Match(str, @"(.+? .+?) ");
if (m.Success)
{
return m.Groups[1].Value;
}
return string.Empty;
}
public string Method4(string str)
{
var regex = new Regex(@"^(.*? .*?) ");
Match m = regex.Match(str);
if (m.Success)
{
return m.Groups[1].Value;
}
return string.Empty;
}
public string Method5(string str)
{
var substring = str.TakeWhile((c0, index) =>
str.Substring(0, index + 1).Count(c => c == ' ') < 2);
return new String(substring.ToArray());
}
100000 次运行的方法执行时间
使用OP的输入:
string value = "o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467";
Method1 took 38ms (00:00:00.0387240)
Method2 took 5ms (00:00:00.0051046)
Method3 took 100ms (00:00:00.1002327)
Method4 took 393ms (00:00:00.3938484)
Method5 took 147ms (00:00:00.1476564)
使用稍长的输入(字符串中的空格):
string value = "o11232.54671232.54671232.54671232.54671232.54671232.5467o11232.54671232.54671232.54671232.54671232.54671232.5467o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467";
Method1 took 71ms (00:00:00.0718639)
Method2 took 20ms (00:00:00.0204291)
Method3 took 282ms (00:00:00.2822633)
Method4 took 541ms (00:00:00.5416347)
Method5 took 5335ms (00:00:05.3357977)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.