Replace multiple characters in a string with "-"

Hey everyone,

I have a simple string question,
I have a variable where I get the input from the user, and what I want to do is if user gives

/ , \ , * , : , ? , " , > , < , | inside the string I want to replace all with " - ".

so for example string= hi/my*\?name<>
after replacement it should be hi-my—name–

Any suggestions?
Maybe some single execution solution?

Heres a solution

  1. assign this to a string variable e.g. “result” , where “input” is your string input,
    this will replace any / , \ , * , : , ? , " , > , < , | that it finds
    System.Text.RegularExpressions.Regex.Replace(input,"[\/\\*:\?\"+Chr(34)+"><|]","-")

Note that Chr(34) means double quote, as you cant just place a double quote inside a string

image

6 Likes

Works like a charm. Thank you

2 Likes

No problem, good luck!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.