حذف حروف خاص
You have a string with a specific set of characters, such as spaces, tabs, escaped single double quotes, any type of punctuation character(s), or some other character(s), at the beginning and/or end of a string. You want a simple way to remove these characters.
private void PruningCharacters()
{
string foo = "--TEST--";
Console.WriteLine(foo.Trim(new char[] {'-'})); // Displays "TEST"
foo = ",-TEST-,-";
Console.WriteLine(foo.Trim(new char[] {'-',','})); // Displays "TEST"
foo = "--TEST--";
Console.WriteLine(foo.TrimStart(new char[] {'-'})); // Displays "TEST--"
foo = ",-TEST-,-";
Console.WriteLine(foo.TrimStart(new char[] {'-',','})); // Displays "TEST-,-"
foo = "--TEST--";
Console.WriteLine(foo.TrimEnd(new char[] {'-'})); // Displays "--TEST"
foo = ",-TEST-,-";
Console.WriteLine(foo.TrimEnd(new char[] {'-',','})); //Displays ",-TEST"
}
منبع : "C# 3.0 Cookbook™, Third Edition"