Steven Spielberg
Steven Spielberg

Reputation:

How to remove leading and trailing spaces from a string

I have the following input:

string txt = "                   i am a string                                    "

I want to remove space from start of starting and end from a string.

The result should be: "i am a string"

How can I do this in c#?

Upvotes: 43

Views: 137480

Answers (9)

ChrisF
ChrisF

Reputation: 137128

String.Trim

Removes all leading and trailing white-space characters from the current String object.

Usage:

txt = txt.Trim();

If this isn't working then it highly likely that the "spaces" aren't spaces but some other non printing or white space character, possibly tabs. In this case you need to use the String.Trim method which takes an array of characters:

  char[] charsToTrim = { ' ', '\t' };
  string result = txt.Trim(charsToTrim);

Source

You can add to this list as and when you come across more space like characters that are in your input data. Storing this list of characters in your database or configuration file would also mean that you don't have to rebuild your application each time you come across a new character to check for.

NOTE

As of .NET 4 .Trim() removes any character that Char.IsWhiteSpace returns true for so it should work for most cases you come across. Given this, it's probably not a good idea to replace this call with the one that takes a list of characters you have to maintain.

It would be better to call the default .Trim() and then call the method with your list of characters.

Upvotes: 88

You Can Use

string txt = "                   i am a string                                    ";
txt = txt.TrimStart().TrimEnd();

Output is "i am a string"

Upvotes: -3

Rich Bryant
Rich Bryant

Reputation: 907

I really don't understand some of the hoops the other answers are jumping through.

var myString = "    this    is my String ";
var newstring = myString.Trim(); // results in "this is my String"
var noSpaceString = myString.Replace(" ", ""); // results in "thisismyString";

It's not rocket science.

Upvotes: 11

senzacionale
senzacionale

Reputation: 20906

 static void Main()
    {
        // A.
        // Example strings with multiple whitespaces.
        string s1 = "He saw   a cute\tdog.";
        string s2 = "There\n\twas another sentence.";

        // B.
        // Create the Regex.
        Regex r = new Regex(@"\s+");

        // C.
        // Strip multiple spaces.
        string s3 = r.Replace(s1, @" ");
        Console.WriteLine(s3);

        // D.
        // Strip multiple spaces.
        string s4 = r.Replace(s2, @" ");
        Console.WriteLine(s4);
        Console.ReadLine();
    }

OUTPUT:

He saw a cute dog. There was another sentence. He saw a cute dog.

Upvotes: 0

Samvel Siradeghyan
Samvel Siradeghyan

Reputation: 3583

Or you can split your string to string array, splitting by space and then add every item of string array to empty string.
May be this is not the best and fastest method, but you can try, if other answer aren't what you whant.

Upvotes: 4

MartyIX
MartyIX

Reputation: 28648

You can use:

  • String.TrimStart - Removes all leading occurrences of a set of characters specified in an array from the current String object.
  • String.TrimEnd - Removes all trailing occurrences of a set of characters specified in an array from the current String object.
  • String.Trim - combination of the two functions above

Usage:

string txt = "                   i am a string                                    ";
char[] charsToTrim = { ' ' };    
txt = txt.Trim(charsToTrim)); // txt = "i am a string"

EDIT:

txt = txt.Replace(" ", ""); // txt = "iamastring"   

Upvotes: 20

Erik B
Erik B

Reputation: 42554

Use the Trim method.

Upvotes: 1

anishMarokey
anishMarokey

Reputation: 11397

text.Trim() is to be used

string txt = "                   i am a string                                    ";
txt = txt.Trim();

Upvotes: 2

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171371

txt = txt.Trim();

Upvotes: 5

Related Questions