C# Tutorial - string.Trim()



Part 38 String Trim

String.Trim() method is used to remove the white space-specific string object.

Trim(Char[])

Removes all leading and trailing occurrences of a set of characters specified in an array from the current string.

 public class Program
    {
        static void Main(string[] args)
        {
            // initialize character 0 to 9 
            char[] data = {'1', '2', '3', '4', '5',
                               '6', '7', '8', '9'};

            string s1 = "123csharptutorialpoint789";

            Console.WriteLine("Before the :" + s1);
//trim
            Console.WriteLine("After   :" + s1.Trim(data));
            Console.ReadLine();
        }

    }

Output

Before the:  123csharptutorialpoint789

After:            csharptutorialpoint


Trim()
Removes all leading and trailing white-space characters from the current string.

public class Program
    {
        static void Main(string[] args)
        {
            // initialize character 0 to 9 
           
            string data = "     csharp-tutorial-point";
            Console.WriteLine("Before  :" + data);
            Console.WriteLine("After   :" + data.Trim());
            Console.ReadLine();
        }

    }
Output :

Before  :     csharp-tutorial-point

After   :csharp-tutorial-point