Link to home
Start Free TrialLog in
Avatar of Ikelca
IkelcaFlag for Canada

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?


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

ASKER CERTIFIED SOLUTION
Avatar of p_davis
p_davis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of p_davis
p_davis

string.Split().Trim()
string[] st2=s.Split(c).Trim();//=)
Avatar of Ikelca

ASKER

string[] st2=s.Split(c).Trim();

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