Solved

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

Posted on 2011-03-04
5
345 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

948 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now