VB.NET function to replace space with a rule

I am looking for following vb.net function (C# code is also fine)

 Convert a string with following rule
   Replace all single space with _
    Trim all the double or more spaces (Still _ should replace the first space )

E.x  My Single space  should become My_Single_Space
       My  DoubleSpace    turns to  My_DoubleSpace       ( Even though there are two spaces, only one _)
       My   TripleSpace singlespace      turns to My_TripleSpace_SingleSpace
Sam OZAsked:
Who is Participating?
 
Rgonzo1971Commented:
Hi,

pls try
s = Regex.Replace(s, " {1,}", "_")

Open in new window

or
s = Regex.Replace(s, "\s+", "_")

Open in new window

REgards
0
 
Rgonzo1971Commented:
do not forget
Imports System.Text.RegularExpressions

Open in new window

0
 
ste5anSenior DeveloperCommented:
Homework, eh? Did you get the obvious test in the test?

Describe in your words first, what you need to implement.
0
 
käµfm³d 👽Commented:
@Rgonzo1971
s = Regex.Replace(s, "\s+", "_")
That replaces more than just spaces.
0
 
Rgonzo1971Commented:
@kaufmed that's why I gave both options didn't know author's definition
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.