Reputation: 1194
in Linq i have somthing like
nodes.Select(td => td.InnerText.
Replace("&", "&").
Substring(0, td.InnerText.IndexOf("+"))).
ToArray();
some times td.InnerText contians a + sign and sometimes it doesn't and if it doesn't i get null reference exception.
how can i apply Substring(0, td.InnerText.IndexOf("+")))
only if td.InnerText contains "+" ?
Upvotes: 1
Views: 699
Reputation: 32681
you can do this
td.InnerText.Replace("&", "&").Substring(0, td.InnerText.IndexOf("+") > -1 ? td.InnerText.IndexOf("+") : 0);
Upvotes: 2
Reputation:
Try using ternary operator. This might not work properly but will give you a hint.
nodes.Select(td => td.InnerText.Replace("&", "&").Substring(0,
(td.InnerText.Contains("+")==true? td.InnerText.IndexOf("+"):string.Empty))).
ToArray();
Upvotes: 1