I’m separating a string by first name and last name.
I equate the last word to the last name. I equate the remainder to the firstname.
This string can be empty or null.
I’m checking for null return.
but in case the string is empty I get the following error.
or I get an error when only the name.
for example; string test = "Jack";
How can I do this in a single line without adding another "if else" control to the code?
https://dotnetfiddle.net/7lr07G
[System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length]
at System.String.Substring(Int32 startIndex, Int32 length)
4
Answers
The problem is
test.LastIndexOf(" ")
which returns -1 if there is no space intest
. Have a look at MSDN – String.SubstringYou can use String.Contains
to check if there is a space in
test
.I recommend using String.IsNullOrEmpty instead of
test != null
.Try with this:
By default, we set string.Empty. If test has a string, we search last whitespace and store in a variable to use later.
If no namespace found: set firstname to all text.
If whitespace found at the end of the string: set firstname to all text without ending spaces. We need to check this case because substring with index+1 fail in this case.
In other case, split the string in that point.
I prefer to use
IndexOf
as little as possible, because I always end up confusing myself with the indices. So here’s different approach:It simply splits using space, then makes the last part the last name and join the others as the first name.
EDIT
I just saw Mighty Badaboom’s solution and was inspired by the use of
Last
. Here’s a version with even fewer integers involved:SkipLast requires .NET standard 2.1. If on older standard you can go for:
Try this one: