Trim user input of spaces, tabs, and others

S

Shabam

Is there a function in dotnet that allows me to trim user input so that:

1) It gets rid of leading/trailing spaces, tabs, newlines and whatever other
non-printing characters
2) Gets rid of all tabs, newlines, and whatever other non-printing
characters anywhere else, preferably replacing them with one space
3) Takes any consecutive number of spaces and replace it with just one
space.
 
N

Niki Estner

Shabam said:
Is there a function in dotnet that allows me to trim user input so that:

1) It gets rid of leading/trailing spaces, tabs, newlines and whatever
other
non-printing characters

String.Trim does that - you can tell it what characters to trim, e.g:
myString = myString.Trim(' ', '\t', '\n', '\r');
2) Gets rid of all tabs, newlines, and whatever other non-printing
characters anywhere else, preferably replacing them with one space
3) Takes any consecutive number of spaces and replace it with just one
space.

I'd suggest using a regular expression for that purpose:
myString = System.Text.RegularExpressions.Regex.Replace("\s+", " ");
Depending on what characters you want to replace you might have to adjust
the search pattern.

Niki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top