Thread: using indexof to remove first two words of string

  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

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I don't use C#, but an improvement I can think of is to define a function that takes a string and how many words you want it to skip, skip them in a loop using that or any other method and return the result.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    What about this? Looks more verbose but because of the error checking, however doesn't create a useless string str.
    Code:
    static void Main (string[] args)
    {
        string word = "Hello World donkey kong";
    
        int firstPos = word.IndexOf(' ');
        if (firstPos > -1 && firstPos < word.Length - 1)
        {
            int secondPos = word.IndexOf(' ', firstPos + 1);
            if (secondPos > -1 &&  secondPos < word.Length - 1)
                word = word.Substring(secondPos + 1);
        }
    
        Console.WriteLine(word);
    }

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Quote Originally Posted by OldGuy2 View Post
    What about this? Looks more verbose but because of the error checking, however doesn't create a useless string str.
    Code:
    static void Main (string[] args)
    {
        string word = "Hello World donkey kong";
    
        int firstPos = word.IndexOf(' ');
        if (firstPos > -1 && firstPos < word.Length - 1)
        {
            int secondPos = word.IndexOf(' ', firstPos + 1);
            if (secondPos > -1 &&  secondPos < word.Length - 1)
                word = word.Substring(secondPos + 1);
        }
    
        Console.WriteLine(word);
    }
    hey thats nice sorry for the delay i was busy.

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