Page 1 of 1

How to return text from a text string

Posted: Thu Sep 17, 2015 1:37 pm
by jmchughsmart
Hi

I just want to return 'Robertsbr1234' from 'varCellItemName'

varCellItemName = "Robertsbr1234, jim";
int varLocation = varCellItemName.IndexOf(",");

I can find the index of "," which is 13 (zero based).
how to just get 'Robertsbr1234'

Thanks

Re: How to return text from a text string

Posted: Thu Sep 17, 2015 2:10 pm
by jmchughsmart
kinda figured it out

string input= varCellItemName;
int stop = input.IndexOf(",");
string output = input.Substring(0, stop);

This displays "Robertsbr1234"

Re: How to return text from a text string

Posted: Thu Sep 17, 2015 2:54 pm
by krstcs
You can also use the "Split()" function of the string class.

string result = "Robertsbr1234, jim".Split(',')[0];

Split() returns an array of strings, and you want the 1st one (0-based index in .NET).

If you want a specific string out of the array, you would need to loop over the array.