Remove wildcard characters from string

Just starting out. I’m iterating a dataTable and want to strip middle initials from names. String.Replace, of course, does not support wildcards.

I.E.:
Gorgeous George
Billy F Gibbons
Marlin Perkins
Abraham van Helsing
Johnny B Goode

To:
Gorgeous George
Billy Gibbons
Marlin Perkins
Abraham van Helsing
Johnny Goode

Ultimately the aim is to invert these and sort on the last name:
George, Gorgeous
Gibbons, Billy
Goode, Johnny
Perkins, Marlin
van Helsing, Abraham

To make this practical, it seems that the first step needs to be to strip out those initials.

ddk

Hey.

So I’m going to assume you need to remove the middle Initial then place last name first with a comma.

I will use row(0).ToString to represent the value for the name inside a ForEach loop:

 Assign row(0) = String.Join(" ",System.Text.RegularExpressions.Regex.Replace(row(0).ToString," [A-Z] "," ").Split(" "c).Skip(1))+", "+System.Text.RegularExpressions.Regex.Replace(row(0).ToString," [A-Z] "," ").Split(" "c)(0)

Basically, I replaced the Initial (assuming it’s always in the pattern " A ", then performed a Split and took the first word and placed it on the end.

I tested this solution with your examples and seemed to work.

Hope this helps.

Regards.

C

3 Likes

Yes!!! That works a charm! Thanks so much. Would’ve taken me forever to dope that out!

ddk