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?


string s = "test, test1, test2";
            string p = ",";
            char[] c = new char[] { ','};
            string[] st2=s.Split(c);
          
            foreach (string match in st2) 
            Console.WriteLine(match);

Open in new window

IkelcaAsked:
Who is Participating?
 
p_davisConnect With a Mentor Commented:
string.Trim() should get rid of the whitespace.
0
 
p_davisCommented:
string.Split().Trim()
0
 
p_davisCommented:
string[] st2=s.Split(c).Trim();//=)
0
 
IkelcaAuthor Commented:
string[] st2=s.Split(c).Trim();

this gave me errors
but i was able to apply trim() to string variable match.trim().

thanks
0
 
p_davisCommented:
i apologize for that...

glad you applied it correctly
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.