CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2012
    Posts
    36

    Need help on a simple split string issue

    Hello,

    I'm trying to split this string up and only get the first word in the array. I only need the first word, in this case "there", how can I split this up.

    I have tried doing these:
    MessageBox.Show(word[0].ToString());// first letter of each word

    MessageBox.Show(s[0].ToString());// I get back for t's

    MessageBox.Show(word.ToString()); //this will return all 4 words, like this there is a cat


    What i'm i doing wrong here.

    Code:
    string s = "there_ is_ a_ cat";
                
                string[] words = s.Split('_');
                foreach (string word in words)
                {
                    MessageBox.Show(word.ToString());
                }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need help on a simple split string issue

    The first thing to do is to learn to use the debugger, then the answer would be pretty obvious. At any rate, words[0] gives you the first word in the array after the s.Split('_') call.

    Okay, back to debugging. If you don't know how to debug, it's simple.

    First you set a breakpoint and then you start the program in debug mode until the breakpoint gets hit. Once it get hit, you can hover over variables and see what their values are.

    Try it on your program by clicking on the foreach loop with the mouse and press F9. You see a red dot appear on the left (this is the breakpoint).

    Next, press F5 to start debugging. The program will stop on the break point. Take the mouse and hover over the words variables. You'll be about to see its contents. Now, press F11 a few times and each time hover over the word variable. Notice how the words variable shows several items, but the word variable only shows one item.

    There are other things that you can do with debugging, but this is the basics. If you try this, you will see how words[0] is the answer you are looking for.

    Once you understand debugging, programming becomes a lot easier.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured