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

nauna
 
on Dec 25, 2019 02:58 AM
Sample_122309.zip
1588 Views

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;
}
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 25, 2019 04:22 AM

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