Remove special characters from string using C# and VB.Net in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

hello,

this expression works well, i want to remove . ,  : * this also should be remove from string

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

Hi nauna,

Refer 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-----