I want to split the column of a datatable like below
Message
--------
PR-111: test message
PR-112 - test message new
PR-113 : test message new2
It should split into a datatable like below
PRNumber Message
------ -----------
PR-111 test message
PR-112 test message new
PR-113 test message new2
Note: There may be space before after the : or -
I have tried below but not giving proper result
var r = dt1.AsEnumerable().Select(g => new {
PRNumber = g["Message"].ToString().Contains(":") == true ? g["Message"].ToString().Split(':')[0] : g["Message"].ToString().Split('-')[0],
Message = g["Message"].ToString().Contains(":") == true ? g["Message"].ToString().Split(':')[0] : g["Message"].ToString().Split('-')[1]
}).ToList();
Can someone help on this please?
Since you want to create a new
DataTablewith two columns, you should create it first:Now a simple
foreachis the best way to fill it, LINQ doesn't help you:Since you have added now some edge cases like these:
It doesn't make any sense to use
string.Splitanymore, you need a simple parser, for example:You can call the parse method in this way: