Is there anyway for me to split a string and exclude the last split?
My data looks like this: data1,data2,data3,
and so if I split the element the last element in the array will be empty, so I would just prefer to exclude it from the split.
Right now I have this:
serialNumbers = delimitedSerials.ToString().Split(',');
Granted I know I can just leave it and in my for
loop just know to skip the last element, but was wondering if there was a simple way to just exclude it at the time of splitting.
您可以使用 StringSplitOptions 参数拆分它:
data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
Below code has a Linq where clause to exclude the empty data
string input ="data1,data2,data3,";
var output = input.Split(',').Where(value => !string.IsNullOrEmpty(value));
foreach(string data in output)
Console.WriteLine(data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.