Advertisement
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. |
//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());
}
}
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
//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));
}
}
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
//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);
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement