- 2
How to extract First & Last name using Regex in C#?
Suppose we have text/string for example: John Doe In the above text/string there can be multiple spaces before, after and in between. How to extract First & Last name and store in two different variables in LOWER Case? Also how to extract first letter of both? Such as, first = john last = doe first_letter = j last_letter = d
4 Réponses
+ 1
string str = "John Doe";
string [] arr=str.Split(" ");
foreach(string s in arr)
{
Console.WriteLine (s);
Console.WriteLine(s[0]);
}
OR LIKE THIS:
string first=arr[0];
string last=arr[1];
Console.WriteLine("FirstName: "+first+"\n"+"LastName: "+last);
Console.WriteLine("FirstLetter: "+first[0]+"\n"+"LastLetter: "+last[0]);
+ 2
You can split your input on space an after additional remove spaces. Super easy.
0
The answer in this thread solves the "multiple spaces inside" issue
https://www.sololearn.com/Discuss/2970583/?ref=app
0
https://code.sololearn.com/cGJRpMcDPRso/?ref=app