Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

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

0
Ikelca
Asked:
Ikelca
  • 4
1 Solution
 
p_davisCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now