简体   繁体   English

如何获取字符串中除第一个单词以外的所有单词

[英]How to get all the word in a string except for the first one

I am trying to split the string in a word array, and get all the word except for the first word. 我试图将字符串拆分成一个单词数组,并获得除第一个单词以外的所有单词。 Like something like this: 像这样的东西:

string s = "Hello World I am on stack overflow";

string result would give me: "World I am on stack overflow" This is what I've tried: string result将给我: "World I am on stack overflow"这是我尝试过的:

 string First = "Hello World, This is First Sentence";
 string words = First.Split(' ');
 string AfterWord = words[First.Length-1];`

There's an overload of String.Split() that does this for you: String.Split()的重载可以为您完成此操作:

string sentence = "Hello World, This is First Sentence";
string words = sentence.Split(' ', 2);
string afterWord = words[1];

[and it's a lot more efficient that joining them back up again afterwards] [并且在以后再次加入他们的效率要高得多]

您可以分割空格,跳过第一个元素,然后将其余元素结合在一起:

string.Join(" ", s.Split(' ').Skip(1));

Try this one: 试试这个:

    String str = "My name is sikander";
    String data[] = str.split('');
    data = data.Where(w => w != data[0]).ToArray();
    String new_str = "";

    for(int i=0; i<data.length(); i++) {
      new_str += data[i];
    }

Hope it works for you..!! 希望这对你有用..!!

Try this one too:- 也试试这个:

string s = "Hello World I am on stack overflow";
            string AfterWord = string.Empty;
            if (s.Length > 0)
            {
                int i = s.IndexOf(" ") + 1;
                AfterWord = s.Substring(i);

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM