tags:

views:

253

answers:

8
string txt = "                   i am a string                                    "

i want to remove space from start of starting and end from string'

like after that it will be "i am a string"

how i can do this in c#

+2  A: 
txt = txt.Trim();
RedFilter
+1  A: 
txt = txt.Trim();
Albin Sunnanbo
+19  A: 

String.Trim

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

Usage:

txt = txt.Trim();

UPDATE

If this isn't working then it would appear that the "spaces" aren't spaces but some other non printing 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

ChrisF
are you sure by doing this i not found "iamastring"
steven spielberg
steven: look at my answer for "iamastring" solution
MartyIX
@steven No, you will find "i am a string"
Sri Kumar
@steven - no this won't remove spaces from the middle of the string. `String.Replace(" ", "")` would do that.
ChrisF
using it i found nothing difference()
steven spielberg
@steven the code by ChrisF would display the string as request by you in the question
Sri Kumar
i do it but it is not worked.
steven spielberg
@steven - in that case the "spaces" aren't spaces but some other non printing character, possibly tabs. In that case you need to use string trim method (http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx) which takes an array of characters
ChrisF
@steven: Post your code. You must do some mistake.
MartyIX
A: 

text.Trim() is to be used

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

Use the Trim method.

Erik B
+1  A: 

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"   
MartyIX
A: 

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.

Samvel Siradeghyan
A: 
 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.

senzacionale