Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wav problem 4 25
User Authentication using Digital Certificate 2 46
C# guarantee sql connection close 6 39
Jquery/JSON passing value from one function to another 12 36
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

770 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