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

nauna
 
on Sep 29, 2021 11:51 PM
Sample_194336.zip
889 Views

hello,

i am using this method to replace special character in string

Problem is if string has value like Gold, silver so its make string like Gold--silver

i want to avoid double dash if it find comma and space in string together or any other replace value so it should have one dash not 2 dash together pls advice.

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

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 30, 2021 12:14 AM

Hi nauna,

Refer below code.

C#

protected void Page_Load(object sender, EventArgs e)
{
    string s = "Gold, silver";
    s = createurlpath(s);
    Response.Write(s);
}

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

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim s As String = "Gold,  silver"
    s = createurlpath(s)
    Response.Write(s)
End Sub

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

Output

Gold-silver