Replace specific characters from string using C# and VB.Net with RegularExpression in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

hello,

i have this method when i pass Nikon camera it returns

Nikon-amera-

its trim c in camera please advice

i just want to replace special character .&?/ any special character should be removed

public string createurlpath(string value)
{
    string returnpath = System.Text.RegularExpressions.Regex.Replace(value, @"[^0-9a-zA-Z]+.", "-");
    return returnpath;
}
You are viewing reply posted by: dharmendr 3 months ago.
Posted 3 months ago

Hi nauna,

Refere below code.

C#

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(createurlpath("Nikon camera.&?/"));
}

public string createurlpath(string value)
{
    string returnpath = System.Text.RegularExpressions.Regex.Replace(value, @"(\s+|\.|&|\?|\/)", "-");
    return returnpath;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Response.Write(createurlpath("Nikon camera.&?/"))
End Sub

Public Function createurlpath(ByVal value As String) As String
    Dim returnpath As String = System.Text.RegularExpressions.Regex.Replace(value, "(\s+|\.|&|\?|\/)", "-")
    Return returnpath
End Function

Output

Nikon-camera----