Title='U.S. History-part 1'
Dim cleanString As String = Regex.Replace(title, "[^a-zA-Z 0-9-/-]", "")
The result I am getting is : US History-part 1 but I need like : US History part 1 . So if there is hiffen that should also replace with blank space.
The title data is dynamic . I have just given a example.
Below is some example of what it should be from left to right.
U.S. History-part 1 => US History part 1
U.S. History.part.1 => US History part 1
U.S- History : part 1 => US History part 1
Loading

Naveen KumarRank #249 · 10K reputationPosted Oct 16, 2024, 12:25 AM EDT
To remove special characters from string, you can modify the regular expression to remove not just special characters, but also replace hyphens and other unwanted characters like periods, colons, etc. with a blank space.
Check the below code:
Imports System.Text.RegularExpressions Dim title As String = "U.S. History-part 1" Dim cleanString As String = Regex.Replace(title, "[^a-zA-Z0-9\s]", " ") cleanString = Regex.Replace(cleanString, "\s+", " ") cleanString = cleanString.Trim() Console.WriteLine(cleanString)Gowtham CpRank #1,552 · 1K reputationPosted Oct 15, 2024, 1:16 PM EDT
Hello Pinku ,
To clean up a string in Visual Basic .NET and remove special characters while replacing hyphens and periods with spaces, you can use the following code:
Imports System.Text.RegularExpressions Dim title As String = "U.S. History-part 1" Dim cleanString As String = Regex.Replace(Regex.Replace(Regex.Replace(title, "[^a-zA-Z0-9.-]", " "), "[.-]", " "), "\s+", " ").Trim() Console.WriteLine(cleanString)In this code, the first
Regex.Replaceremoves unwanted special characters. The secondRegex.Replacereplaces hyphens and periods with spaces. The thirdRegex.Replaceconsolidates any multiple spaces into a single space, andTrim()ensures there are no leading or trailing spaces. This method effectively cleans up the string to meet your requirements.Thanks!