HelloWorld1337
HelloWorld1337

Reputation: 191

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 First = "Hello World, This is First Sentence";
 string words = First.Split(' ');
 string AfterWord = words[First.Length-1];`

Upvotes: 2

Views: 7090

Answers (4)

Gowtham Alan
Gowtham Alan

Reputation: 234

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

            }

Upvotes: 0

Shaikh Sikander
Shaikh Sikander

Reputation: 3

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..!!

Upvotes: 0

Ian Mercer
Ian Mercer

Reputation: 39277

There's an overload of String.Split() that does this for you:

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]

Upvotes: 13

bcwhims
bcwhims

Reputation: 2875

You can split on spaces, skip the first element and join the remaining elements together:

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

Upvotes: 5

Related Questions