[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

How can I reformat a name ("Doe, John D") to remove middle initial and comma, and show only first name, then last name? ("John Doe")

Well the subject pretty much sums it up.. Can anyone show me the best way to do this using C#?

Thanks much,

oddic
0
OddiC
Asked:
OddiC
  • 2
1 Solution
 
dkloeckCommented:
You can use Regular expressions like this:

string testString = "Doe, John D";

            Regex firstNameRegex = new Regex(",.+ ");
            Regex lastNameRegex = new Regex(".+,");

            string firstName = firstNameRegex.Match(testString).Value.Substring(1).Trim(); ;
            string lastName= lastNameRegex.Match(testString).Value.Substring(0,lastNameRegex.Match(testString).Value.Length-1).Trim();

            textBox1.Text = firstName +" "+ lastName;
0
 
OddiCAuthor Commented:
Awesome. Thank you dkloeck!
0
 
dkloeckCommented:
no problem ^_^
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now