vijay anand
vijay anand

Reputation: 19

How to separate name and surname

I have code name and surname put into same string with coma in the middle ,as "JohnSmith"

I need to insert into database to separate

Can you show me how to code that please.

Thanks

vijay

Upvotes: 0

Views: 1103

Answers (3)

Mark W
Mark W

Reputation: 811

This is easy in most cases, but in a production environment the edge cases kill you. I like the string.split answer, but the incredible variation in how names are formated means more thinking and more code.

Upvotes: 0

Eoin Campbell
Eoin Campbell

Reputation: 44308

Presumably your asking how to split up a single string where the names are seperated by a comma ','

If that's the case, you can split a string using the Split(char x) method. Then you can use each part what ever way you want.

string x = "John,Smith";

string [] parts = x.Split(',');


if(parts.Length == 2)
{
   string firstName = parts[0];
   string secondName = parts[1];
}

Something like that.

Upvotes: 5

user302845
user302845

Reputation:

Vijay, how about at least trying to google stuff like this by yourself? As in "C# split string"? Hundreds of decent results come up. Heck, there are tons of these examples on SO as well.

Other people can't do your work for you, so how about actually putting in some effort into learning process instead of relying on others to do mundane things for you?

Upvotes: 2

Related Questions