If I have 5 String variables and between 0 and 5 of them are null or empty is there an easy/short way of returning the first one that is not null or empty? I am using .NET 3.5
var myString = new string[]{first, second, third, fouth, fifth}
.FirstOrDefault(s => !string.IsNullOrEmpty(s)) ?? "";
//if myString == "", then none of the strings contained a value
edit: removed Where(), placed predicate in FirstOrDefault(), thanks Yuriy
.FirstOrDefault(x=> !string.IsNullOrEmpty(x))
with an optional ?? string.Empty
if you don't want nulls. –
Extravagate Define an extension method:
static string Or(this string value, string alternative) {
return string.IsNullOrEmpty(value) ? alternative : value;
}
Now you can say the following:
string result = str1.Or(str2).Or(str3).Or(str4) …
FirstOrDefault
method will in fact stop after the first element is found. If you're only comparing 5 elements, though, I wouldn't worry about it. –
Landslide string.Format("Displaying item {0}", item.Name.Or("(no name given)"));
–
Extravagate private static string FirstNonEmptyString(params string[] values)
{
return values.FirstOrDefault(x => !string.IsNullOrEmpty(x));
}
Called like this:
Console.WriteLine(FirstNonEmptyString(one, two, three, four, five) );
If you have a fixed number of variables I would suggest to use the null coalescing operator to check for nulls. This is by far the easiest option.
var result = s1 ?? s2 ?? s3 ?? s4 ?? s5 ?? "";
There is in my opinion no need to do a separate function for that anymore given there is a language construct for it already.
Edit: Empty strings are considered non-null and thus if one of them is empty it will return it.
so if you want to use this together:
public static string EmtpyToNull(string value)
{
return string.IsNullOrEmpty(value) ? null : value;
}
var result = EmptyToNull(s1)
?? EmptyToNull(s2)
?? EmptyToNull(s3)
?? EmptyToNull(s4)
?? EmptyToNull(s5)
?? "";
If your strings are in an IEnumerable
you can use First
or FirstOrDefault
:
strings.First(s => !string.IsNullOrEmpty(s))
First
throws an IOE if all strings are null or empty –
Extravagate First
throwing is something people often don't realize and it ends up biting them in the ass. Thought it might be important for people reading to see that. –
Extravagate If you're using .NET 3.5 this is very easy with Linq.
string[] strings = new[] {"", "a", "b", ""};
string firstNotNullOrEmpty =
strings.Where(s => !String.IsNullOrEmpty(s)).FirstOrDefault();
First
has an override which makes the Where
pointless. Also, if all strings are null you'd throw an InvalidOperationException here. Which would suck ass. –
Extravagate InvalidOperationException
? –
Basipetal (new string[] { (string)null }).First(x=>!string.IsNullOrEmpty(x))
You'll see it throws an IOE, as First() is designed to do if there are no matching results. FirstOrDefault() never throws if no matching results are found; it just returns default(T)
. –
Extravagate First()
to FirstOrDefault()
=). –
Basipetal var found = new[]{first, second, third, fourth, fifth}.FirstOrDefault(x =>!String.IsNullOrEmpty(x));
Yes.
string selected = null;
foreach(string currStr in strArray)
if(String.IsNullOrEmpty(currStr)==false)
{
selected = currStr;
break;
}
No expression trees, no LINQ abuse, no obscure language features. Same runtime performance and a first year CS student can figure out what you're doing.
false
(ugh!) any better than a concise, self-explanatory query that fits on one line? –
Olden © 2022 - 2024 — McMap. All rights reserved.