Advertisement



< Prev
Next >



C# Trim() method




The Trim() method of String class can be used to perform either of the two functions:




Some overloads of Trim() method



Methods Description
Trim()
This method trims all the white space characters present at the beginning and at the end of invoked String.

Trim(char [ ])
This method trims a set of characters specified in the char[ ] array, from the beginning and at the end of the invoked String.






Example of Trim() method


In the upcoming program, we are going to create two String objects that contain some white spaces at the beginning and at the end, and we will call the first version of the Trim() method on each of these String objects to trim all these leading and trailing white spaces.

//C# Calling the Trim() method to trim the leading and trailing blank spaces from a String.


using System;

class StringTrim
{
public static void Main()
{
	//Creating the first String object 
	String str1 = "         Hello            ";
	
	//Creating a char array
	char[] arr = {' ', ' ', ' ', 'H', 'e', 'y', ' ', ' ', ' '};

	//Creating a String object from a char[] array.
	String str2 = new String(arr);

	
	//Printing the original value of the first String
	Console.WriteLine("Original value of the first String: "  + str1);

	//Printing the trimmed copy of the first String
	Console.WriteLine("The trimmed value of first String: " + str1.Trim());


	//Printing the original value of the second String
	Console.WriteLine("Original value of the second String: "  + str2);

	//Printing the trimmed copy of the first String
	Console.WriteLine("The trimmed value of second String: " + str2.Trim());

}
}


Output is :


Original value of the first String:          Hello
The trimmed value of first String: Hello
Original value of the second String:    Hey
The trimmed value of second String: Hey





Example of Trim(char [ ]) method


In the next program, we are going to explain the second version of Trim() method which trims a set of characters specified in the char[ ] array, from the beginning and at the end of the invoked String and returns its trimmed copy. If this method couldn't find the characters specified in the char[] array around the edges of the invoked String, then it returns null.

Note: This version of Trim() method performs a case-sensitive trimming i.e. while trimming, the cases of the characters specified in the char[] array are followed. Let us show you in an example.

//C# Calling the Trim(char[]) method to trim the leading and trailing blank spaces from a String.


using System;

class StringTrim
{
public static void Main()
{
	//Creating the first String object 
	String str1 = "An amazing day it is";
	

	//Creating the second String object
	String str2 = "a new day has begun";

	
	//Creating a char array that contains characters to trim from String objects.
	char[] arr = {'a', 'n', 's', 'e'};



	//Printing the original value of the first String
	Console.WriteLine("Original value of the first String: "  + str1);


	//Printing the trimmed copy of the first String
	Console.WriteLine("The trimmed value of first String: " + str1.Trim(arr));


	//Printing the original value of the second String
	Console.WriteLine("Original value of the second String: "  + str2);


	//Printing the trimmed copy of the first String
	Console.WriteLine("The trimmed value of second String: " + str2.Trim(arr));

}
}


Output is :


Original value of the first String: An amazing day it is
The trimmed value of first String: An amazing day it i
Original value of the second String: a new day has begun
The trimmed value of second String:  new day has begu
content of String object, minus the leading and trailing white spaces in it. It does not modify the original content of a String object.


Advertisement




The Trim() method does not modify the original content in a String object


Either version of the Trim() only returns the trimmed copy of the invoked String object i.e. the method Trim() does not modify the original content of a String object. Let's understand this by an example.

//C# Calling the Trim(char[]) method to trim the leading and trailing blank spaces from a String.


using System;

class StringTrim
{
public static void Main()
{
	//Creating the first String object 
	String str1 = "       how is your day going on?            ";
	

	//Creating the second String object
	String str2 = "a new day has begun";

	
	//Creating a char array that contains characters to trim from String objects.
	char[] arr = {'a', 'n', 's'};



	//Printing the original value of the first String
	Console.WriteLine("Original value of the first String: "  + str1);


	//Calling the first version of the Trim() method which removed the leading and trailing white spaces
	Console.WriteLine("The trimmed value of first String: " + str1.Trim());


	//Printing the value of the first String after calling Trim() on it.
	Console.WriteLine("First string after calling the Trim() method : " + str1);

	
	//Printing the original value of the second String
	Console.WriteLine("Original value of the second String: "  + str2);


	//Printing the trimmed copy of the first String
	Console.WriteLine("The trimmed value of second String: " + str2.Trim(arr));


	//Printing the value of the second String after calling Trim() on it.
	Console.WriteLine("Second string after calling the Trim() method : " + str2);
}
}


Output is :


Original value of the first String:        how is your day going on?
The trimmed value of first String: how is your day going on?
First string after calling the Trim() method :        how is your day going on?
Original value of the second String: a new day has begun
The trimmed value of second String:  new day has begu
Second string after calling the Trim() method : a new day has begun

Each String object contains the same original value even after the method Trim() was called on it, because the method Trim() only returns the trimmed copy of the invoked String object.




Please share this article -





< Prev
Next >
< C# Concat() Method
C# ToUpper() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement