views:

53

answers:

2

Hi All-

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,

+1  A: 

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]);
Rob
It preserves the space after comma (LastName,[space]FirstName), doesn't it?
MartyIX
A couple of Trim() calls in there ought to clean that right up.
Joel Etherton
I would prefer this solution with splitting on the comma char in combination with `Trim` calls over splitting on the string `", "`, since it would gracefully handle if any input happens to come as `"Firstname,Lastname"`, without the space (unless that is to be considered an error case).
Fredrik Mörk
@Fredrik, good point.. I hadn't thought of that when I put my answer together =)
Rob
awesome, thanks for the assist!!!
bluefeet
@bluefeet, no problems, glad I could help :)
Rob
@Joel: Sure. It's just what OP wants :)
MartyIX
A: 

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);
MartyIX