howto do multiple split values from a list of string to an object, Blows if there is no string to split
I want to split this
List<string> CrossReference = new List<string>
{
"task #1443; task #1447; task #1444; task #973; rel #33; sprint #815",
""
};
First by
;
then by
#
I have this class
public class CrossReference
{
public long Aid { get; set; }
public string TaskType { get; set; }
}
and I have this test
[TestMethod]
public void splitTest()
{
List<string> CrossReference = new List<string>
{
"task #1443; task #1447; task #1444; task #973; rel #33; sprint #815",
""
};
foreach (var str in CrossReference)
{
var str1 = str.Trim().Split(';')
.Select(x => new CrossReference
{
TaskType = (string)x.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)[0],
Aid = Convert.ToInt64(x.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)[1]),
}).ToList();
}
}
The value of str1 in the first run is ok, but in the second run blows up because I don´t have anything to split.
How can I prevent the case that there is no string to split.
Thanks in advance
Jolynice
You can check str is empty or have just white spaces
if(!string.IsNullOrWhiteSpace(str))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments