Trimming a string.
You must have always come across a situation where you may need to trim a string in order to attain a specific result or to use the string for a purpose which need a part of the string trimmed from either the start or the end of the string.
C# provides a specific function just for this purpose to make your life easy.
The System.String class is specifically for storing the string objects. and has a lot of built-in methods defined to help the programmer to perform operations on the strings.
The Trim() method is used to remove the white spaces from a string from the leading or the end of the string.
when we apply this method on any string , any leading or trailing white spaces are been trimmed and the string is returned.
The Trim has a overloaded method that takes a character array as an input parameter.
Hence Trim('a','b','c') will trim any appearance of the character a or b or c in the leading or the trailing end of the string.
The TrimStart('a',b','c') will trim the string of the characters only which occur at the start of the string and TrimEnd('a','b') will trim the trailing end of the string only.
Note: all the trim functions mentioned above return a string value. Hence their return type is string.
The code is as follows:
using System;
namespace Logiphix
{
class Program
{
static void Main(string[] args)
{
string s = " This string will be trimmed ";
Console.WriteLine(s);
s = s.Trim(); //// This will remove all the Leading and trailing whitespaces from the string
Console.WriteLine(s);
s = s.Trim('T'); //// This will remove all the Leading and trailing character 'i' appearances
Console.WriteLine(s);
s = s.TrimStart('h'); //// This will remove all the Leading occourace of the character 'h'
Console.WriteLine(s);
s = s.TrimEnd('d'); //// This will remove all the trailing character 'd' from the string
Console.WriteLine(s);
Console.ReadKey();
}
}
}
The output will be :
Please note that the previous trimming is retained even in the next trim , this is because we store the value in s each time we make the operation hence s gets a new trimmed string value in it each time.
You must have always come across a situation where you may need to trim a string in order to attain a specific result or to use the string for a purpose which need a part of the string trimmed from either the start or the end of the string.
C# provides a specific function just for this purpose to make your life easy.
The System.String class is specifically for storing the string objects. and has a lot of built-in methods defined to help the programmer to perform operations on the strings.
The Trim() method is used to remove the white spaces from a string from the leading or the end of the string.
when we apply this method on any string , any leading or trailing white spaces are been trimmed and the string is returned.
The Trim has a overloaded method that takes a character array as an input parameter.
Hence Trim('a','b','c') will trim any appearance of the character a or b or c in the leading or the trailing end of the string.
The TrimStart('a',b','c') will trim the string of the characters only which occur at the start of the string and TrimEnd('a','b') will trim the trailing end of the string only.
Note: all the trim functions mentioned above return a string value. Hence their return type is string.
The code is as follows:
using System;
namespace Logiphix
{
class Program
{
static void Main(string[] args)
{
string s = " This string will be trimmed ";
Console.WriteLine(s);
s = s.Trim(); //// This will remove all the Leading and trailing whitespaces from the string
Console.WriteLine(s);
s = s.Trim('T'); //// This will remove all the Leading and trailing character 'i' appearances
Console.WriteLine(s);
s = s.TrimStart('h'); //// This will remove all the Leading occourace of the character 'h'
Console.WriteLine(s);
s = s.TrimEnd('d'); //// This will remove all the trailing character 'd' from the string
Console.WriteLine(s);
Console.ReadKey();
}
}
}
The output will be :
Please note that the previous trimming is retained even in the next trim , this is because we store the value in s each time we make the operation hence s gets a new trimmed string value in it each time.
No comments:
Post a Comment