Taryn
Taryn

Reputation: 247680

Winforms C# change string text order

I am new to C# and I get the user's name that is system generated and it comes in the format:

LastName, FirstName

I want to change this to be added to a database

FirstName.LastName

I am totally stuck on how to do this, any help would be great,

Upvotes: 1

Views: 943

Answers (2)

MartyIX
MartyIX

Reputation: 28648

Try this:

    string username = "LastName, FirstName";
    string[] words = username.Split(new string[]{", "});
    string result = words[1] + "." + words[0]; // storing


    // for output
    Console.WriteLine("{0}.{1}", words[1], words[0]);
    Console.WriteLine(result);

Upvotes: 0

Rob
Rob

Reputation: 45771

If the order always comes as "Lastname, Firstname", the following code should work:

var variableContainingLastNameFirstName = "LastName, FirstName";

var split = variableContainingLastNameFirstName.Split(new char[] {',' });
var firstNamelastName = string.Format("{0}, {1}", split[0], split[1]);

Upvotes: 1

Related Questions