17 November 2013

what is trim() in c#

Trim eliminates leading and trailing whitespace. We need to remove whitespace from the beginning or ending of a string. We use the .NET Framework's Trim method to do this efficiently. This method removes any characters specified.

 

Trim Example input & output


1)      String input:       "   This is an example string. "
      Trim method Output: "This is an example string."
 
2)       String input:       "This is an example string.\r\n\r\n"
      Trim method Output: "This is an example string."
 
3)       String input:       "\t, again and again."
      Trim method Output:   "again and again."
Note:  You can remove commas, spaces, and tabs.

Example

This first example removes space characters from the beginning and end of a C# string. Note that when you call Trim on a string, it copies the string and returns a modified version of that copy. The original is not changed.
Example program for trim in c#
 
using System;
class Program
{
    static void Main()
    {
             // Input string
             string st = "  This is an example string. ";
 
             // Call Trim instance method.
             // This returns a new string copy.
             st = st.Trim();
 
             Console.WriteLine(st);
    }
}
 
Output:-(Spaces were removed.)
 
This is an example string.

No comments:

Post a Comment