How to extract First & Last name using Regex in C#? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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

7th Apr 2022, 6:59 AM
Jojan
4 Answers
+ 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]);
10th Apr 2022, 2:25 AM
Jackie
Jackie - avatar
+ 2
You can split your input on space an after additional remove spaces. Super easy.
7th Apr 2022, 7:14 AM
JaScript
JaScript - avatar
0
The answer in this thread solves the "multiple spaces inside" issue https://www.sololearn.com/Discuss/2970583/?ref=app
7th Apr 2022, 8:21 AM
Ipang
0
https://code.sololearn.com/cGJRpMcDPRso/?ref=app
7th Apr 2022, 8:30 AM
Jojan