Adjit Adjit - 1 year ago 143
C# Question

C# split string and exclude last split

Is there anyway for me to split a string and exclude the last split?

My data looks like this:

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 = delimittedSerials.ToString().Split(',');

Granted I know I can just leave it and in my
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.

Answer Source

you can split it using the StringSplitOptions parameter:

data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download