2010-04-08 57 views
19

我有一个这样的字符串:如何从字符串中提取子字符串,直到遇到第二个空格?

"o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467"

如何只提取"o1 1232.5467"

要提取的字符数总是不一样。因此,我只想提取,直到遇到第二个空间。

+1

单词之间总会有一个空格吗?如果不是,下面的大多数解决方案都是无效的。 – 2010-04-08 17:44:16

回答

4

使用正则表达式:。

Match m = Regex.Match(text, @"(.+? .+?) "); 
if (m.Success) { 
    do_something_with(m.Groups[1].Value); 
} 
46

直接的方法是如下:

string[] tokens = str.Split(' '); 
string retVal = tokens[0] + " " + tokens[1]; 
18

只需使用String.IndexOf两倍于:

 string str = "My Test String"; 
    int index = str.IndexOf(' '); 
    index = str.IndexOf(' ', index + 1); 
    string result = str.Substring(0, index); 
+1

...如果您从字符串的末尾搜索(LastIndexOf),则可以在第三行中替换'index-1'。 – Jess 2011-02-11 20:38:21

+0

我更喜欢这种方法,因为它的效率。 – Panzercrisis 2015-01-11 00:11:20

8

获得第一个空间的位置:

int space1 = theString.IndexOf(' '); 

n的位置之后分机空间:

int space2 = theString.IndexOf(' ', space1 + 1); 

获取字符串的一部分到所述第二空间:

string firstPart = theString.Substring(0, space2); 

上面的代码把togehter成一衬垫:

string firstPart = theString.Substring(0, theString.IndexOf(' ', theString.IndexOf(' ') + 1)); 
1
string[] parts = myString.Split(" "); 
string whatIWant = parts[0] + " "+ parts[1]; 
+0

@Jeff:哪些特别? – 2010-04-08 13:18:02

+0

对变量使用Pascal情况; 'MyString'和'WhatIWant'。 – 2010-04-08 13:34:34

+0

也许MyString是属性。无论如何,我不会将这些规则应用于这样的片段。 – 2010-04-08 13:37:27

5
s.Substring(0, s.IndexOf(" ", s.IndexOf(" ") + 1)) 
1
string testString = "o1 1232.5467 1232.5467........."; 

string secondItem = testString.Split(new char[]{' '}, 3)[1]; 
3

事情是这样的:

int i = str.IndexOf(' '); 
i = str.IndexOf(' ', i + 1); 
return str.Substring(i); 
+0

这是一个相当简单,高效和聪明的解决方案:) – Siewers 2012-11-30 13:32:03

0

这样做有它像其他人所说的更短的方式,但你也可以检查每个字符,直到你遇到自己的第二空间,然后返回相应的字符串。

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); 
} 
0

您可以尝试先找到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(' '); 
1

我会建议一个正则表达式这一点,因为它处理你可能没有考虑的情况。

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)); 
} 
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()); 

我也得到了@浩的答案,并把它做成一个周期,因此,只要你想,你可以再次使用它的很多的话: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); 
0

我在想这个问题,我自己的代码,而且即使我可能最终会使用更简单/更快的东西,这是另一个类似于@Francisco a的Linq解决方案dded。

我只是喜欢它,因为它读取最喜欢你真正想要做的事情:“在产生的子字符串少于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()); 
相关问题