Ikelca
asked on
how to tokenize a string and trim each string in c#
hi guys,
im new to c# and currently learning
and i found there is no stringTokenizer in c# compared with java.
but i was able to accomplish it by string.split()
however, the result is slightly off and wondering how i can fix it.
for example, see following code
the result is
test
test1
test2
so there always a space in front of test1 and test2.
and i only want following result
test
test1
test2
how can i get above result?
im new to c# and currently learning
and i found there is no stringTokenizer in c# compared with java.
but i was able to accomplish it by string.split()
however, the result is slightly off and wondering how i can fix it.
for example, see following code
the result is
test
test1
test2
so there always a space in front of test1 and test2.
and i only want following result
test
test1
test2
how can i get above result?
string s = "test, test1, test2";
string p = ",";
char[] c = new char[] { ','};
string[] st2=s.Split(c);
foreach (string match in st2)
Console.WriteLine(match);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
string.Split().Trim()
string[] st2=s.Split(c).Trim();//=)
ASKER
string[] st2=s.Split(c).Trim();
this gave me errors
but i was able to apply trim() to string variable match.trim().
thanks
this gave me errors
but i was able to apply trim() to string variable match.trim().
thanks
i apologize for that...
glad you applied it correctly
glad you applied it correctly