Thread: using indexof to remove first two words of string

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    using indexof to remove first two words of string

    my code words but its ugly;

    Code:
    string word = "Hello World donkey kong";
                if (word.Length > 0)
                {
                    int i = word.IndexOf(" ") + 1;
                    string str = word.Substring(i);
                    int j = str.IndexOf(" ") + 1;
                    string str_2 = str.Substring(j);
                    Console.WriteLine(str_2);
                }
    it prints "donkey kong" to the console screen which is what i want. can you show me the correct way to do this with cleaner code because this is really hairy right now.
    Last edited by jeremy duncan; 08-03-2017 at 01:41 AM. Reason: str_2 was using i so i changed it to j

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-10-2012, 12:24 AM
  2. Replies: 2
    Last Post: 07-23-2011, 10:26 PM
  3. Replies: 7
    Last Post: 10-01-2010, 04:09 PM
  4. remove duplicate words from linked list
    By rocketman03 in forum C Programming
    Replies: 8
    Last Post: 11-22-2008, 07:47 PM
  5. remove value from string
    By palku in forum C Programming
    Replies: 4
    Last Post: 10-01-2005, 09:15 PM

Tags for this Thread