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?
Printable View
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?
look at the split method
Code:string yourString="man:woman";
string[] strArray = yourString.Split(':');
//strArray[0] contains 'man'
//strArray[1] contains 'woman'
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?
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.
Wont this code:
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?Code:string yourString="man:woman";
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.
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.
I did a code that at the end will display a messagebox like this:Code:string yourString="man:woman";
string[] strArray = yourString.Split(':');
And it popped up with man not the item in my listBox.Code:MessageBox.Show("Logged into:" + strArray[0]);
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.
So how would u reference it to my listbox. I tried a few pieces of code but got errors
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
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
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.Code:string yourString = "man:woman:unknown";
string[] split = yourString.Split(':');
// split[0] = "man"
// split[1] = "woman"
// split[2] = "unknown"
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.
Alright thanks for all the help. I set up the loop :)