CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Sep 2011
    Posts
    69

    Question How to split a string?

    How can I split this phrase at the colon: "man:woman"

    I am importing text into a listbox in the format above and want to get the text only from man and later the text only from woman. How would I call it?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to split a string?

    look at the split method
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How to split a string?

    Code:
    string yourString="man:woman";
    string[] strArray = yourString.Split(':');
    
    //strArray[0] contains 'man'
    //strArray[1] contains 'woman'
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Sep 2011
    Posts
    69

    Re: How to split a string?

    What bout if Tay phrase changes around so by man:woman I meant that it'll show a man and womans name. Instead of saying "man:woman" how can I make it work directly with a listbox?

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to split a string?

    Split will split your string based on the character you provide and put the results in your array var. It doesn't matter if it is the word man or the mans name whatever is there is what you will get.

    Now if you are looking to parse out the word man and replace it with the mans name that is a completely different question.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Sep 2011
    Posts
    69

    Re: How to split a string?

    Wont this code:

    Code:
    string yourString="man:woman";
    Only split any word at the colon that says man:woman? What if says jake:trina and the next item in the listbox says micheal:jacky , would it still split it at the colon?

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to split a string?

    The code you posted will not split anything as it does not include a call to the split method it is simply a string that will stay as is.

    Clearly you have not bothered to try what was suggested. Split() splits a string into an array based on a given seperator character. It does not matter what is in the string. If you had

    Harry :::::::::::: Ape

    You would get an array with several elments all but the first and the last would be blank Harry in the first Ape in the last.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Sep 2011
    Posts
    69

    Re: How to split a string?

    Are you sure I just did some practice code and this is what Im doing becuase as you said it doesnt matter whats in the string.

    Code:
    string yourString="man:woman";
    string[] strArray = yourString.Split(':');
    I did a code that at the end will display a messagebox like this:

    Code:
    MessageBox.Show("Logged into:" + strArray[0]);
    And it popped up with man not the item in my listBox.

  9. #9
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: How to split a string?

    If you are copying the code exactly then no matter what your result will always be "man" since this is in your code yourString = "man:woman"

    Think of the split as different arrays depending on your split character. In this case you are using : as your split character. So if your string contained
    Code:
    	string yourString = "man:woman:unknown";
    	string[] split = yourString.Split(':');
    
    	// split[0] = "man"
    	// split[1] = "woman"
    	// split[2] = "unknown"
    From the example you see each time a : is present the array ends at that spot and a new position is started until another : is located or the end of the string is reached.

    Sorry for my bad explanation but once you understand how a split works it makes things really simple.

    I am guessing from the looks of it that you are copying the code exactly as DataMister posted. The problem with that is that he is defining a value for yourString and therefore it will always come up as man in your testing.

    In order for it to work the way you want it you must get each item from the listbox and then split the item to get the word you want. You are very close to getting the solution all you need is to understand split and then learn how to get each item from your listbox through a loop since it looks like you want multiple items with a single click.

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to split a string?

    Of course I am sure the string you are splitting contains the word man in the first postion so that is what you will get.
    If it contained Adam you would get Adam. It will not automagically get data from a different string than the one you tell it to.

    If you want it to split items in a list box then you have to tell it to do that. It will only do what you tell it to.
    Last edited by DataMiser; September 14th, 2011 at 06:54 PM.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Sep 2011
    Posts
    69

    Re: How to split a string?

    So how would u reference it to my listbox. I tried a few pieces of code but got errors

  12. #12
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to split a string?

    You have to reference an item in the list box or use a loop and refernce several items one at a time depending on what you want to do.

    You should look at your online help and check out the examples for Split() and Listbox
    Always use [code][/code] tags when posting code.

  13. #13
    Join Date
    Sep 2011
    Posts
    69

    Re: How to split a string?

    Alright thanks for all the help. I set up the loop

Tags for this Thread

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