How to replace multiple characters from string using C# in ASP.Net

Last Reply one year ago By zameer

Posted one year ago

Hi sir,

Below code i am using to replace string but i want to replace multiple string with the same logic how to acheive it.

 protected string GetTitle(object obj)
    {
        return obj.ToString().Replace(' ', '-');

    }

Thanks & Regards

firoz khan

 

Posted one year ago

Hi firoz1986,

Please explain with example with some more details


Posted one year ago

Hi firoz1986,

As per  you query i have created one sample.Please refer the below sample.If you have any doubt please revert me back.

Code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        char[] options = new char[] { ' ', '+', '*', '/', '?', '|' };
        string outputstring = GetTitle("This is test+example of*replace/of?multiple|string.", options);
        Response.Write(outputstring);
    }
}

protected string GetTitle(object obj, char[] options)
{
    foreach (char o in options)
    {
        obj = obj.ToString().Replace(o, '-');
    }

    return obj.ToString();
}