Thursday, October 9, 2008

Remove some characters from string in C#

namespace RemoveCharacters
{
class Program
{
static void Main(string[] args)
{
string[] htmlComments = new string[] {"",
"",
"",
""};
char[] commentChar = new char[] { '!','-','<','>'};

Original(htmlComments);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~");
RemoveCommentCharacter(htmlComments,commentChar);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~");
RemoveWithReplace(htmlComments,commentChar);
}

public static void Original(string[] str)
{
for (int i = 0; i <= str.GetUpperBound(0); i++)
{
Console.WriteLine("Comments: "+str[i]);
}
}

public static void RemoveCommentCharacter(string[] str,char[] ch)
{
for (int i = 0; i <= str.GetUpperBound(0); i++)
{
str[i] = str[i].Trim(ch);
str[i] = str[i].TrimEnd(ch);
Console.WriteLine("Comments: " + str[i]);
}
}

public static void RemoveWithReplace(string[] str,char[] ch)
{
for (int i = 0; i <= str.GetUpperBound(0); i++)
{
for (int j = 0; j < ch.Length; j++)
{
str[i] = str[i].Replace(ch[j].ToString(),"");
}
Console.WriteLine("Comments: " + str[i]);
}
}
}
}

No comments: