Solved

how to tokenize a string and trim each string in c#

Posted on 2011-03-04
5
348 Views
Last Modified: 2012-05-11
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
Comment
Question by:Ikelca
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
p_davis earned 250 total points
ID: 35038799
string.Trim() should get rid of the whitespace.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35038803
string.Split().Trim()
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35038810
string[] st2=s.Split(c).Trim();//=)
0
 

Author Comment

by:Ikelca
ID: 35038890
string[] st2=s.Split(c).Trim();

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

thanks
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35038897
i apologize for that...

glad you applied it correctly
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question