Friday, July 31, 2009

String operations in C# ????

How can I do the following :


if for example i have a string entered by the user in this format:


AX, BX


or AX , BX


or AX ,BX





what can i do so that i can neglect any spaces eneterd by the user so that each time whatever the user enters it will look like this : AX,BX ???

String operations in C# ????
What you want to do is to replace all white spaces with the empty string. Here is how you can do that:





string something = "AX , BX";





something = something.Replace(" ", "");





// or


something = something.Replace(" ", string.Empty);
Reply:1. call a string tokenizer with the parameter(,).


2. as you read each token trim it for leading and trailing white spaces


3. to a common string append the token and "," to it incrementally for all the tokens.
Reply:string myString = "AX, BX";


myString = myString.Replace(" ","");


No comments:

Post a Comment