• Top Posts

    Trim() method in C#

    • In c#, the string Trim() method is used to remove all trailing and leading white space characters from the specified string object.
    • By using Trim() method , we can also remove all occurrences of specified characters from the start and end of the current string .
    • Trim() removes the specified characters and string also.

      class Trim  
       {  
         static void Main(string[] args)  
         {  
           // Trim Whitespaces  
           string str1 = " Welcome";  
           string str2 = "  to  ";  
           string str3 = "  Tutlane";  
           Console.WriteLine("Before Trim: {0} {1} {2}", str1, str2, str3);  
           Console.WriteLine("After Trim: {0} {1} {2}", str1.Trim(), str2.Trim(), str3.Trim());  
           char[] trimChars = { '*', '@', ' ' };  
           // Trim with Characters  
           string str4 = "@@** Suresh Dasari **@";  
           Console.WriteLine("Before Trim: {0}", str4);  
           Console.WriteLine("After Trim: {0}", str4.Trim(trimChars));  
           Console.WriteLine("\nPress Enter Key to Exit..");  
           Console.ReadLine();  
         }  
       }  
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728