Reputation:
I have the following expression:
"<p>What ?</p>\n<pre>Starting Mini</pre>"
When I perform a split as follows:
var split = content
.Split(new[] { "<pre>", "</pre>" }, StringSplitOptions.None);
Then it gives me three entries:
"<p>What ?</p>\n"
"Starting Mini"
""
Why does it give an empty line as the third entry and how can I avoid this?
Upvotes: 5
Views: 747
Reputation: 1101
You get this behaviour as specified from Microsoft: "Adjacent delimiters yield an array element that contains an empty string ("")." So since you have the last pre you get the last empty array element
Upvotes: 2
Reputation: 498
You are getting empty line due to
</pre>
You are instructing split
function to split by <pre>
and </pre>
As result with <pre>
you are getting
<p>What ?</p>\n
Starting Mini</pre>
And next result is with </pre>
is
<p>What ?</p>\n
Starting Mini
...
Upvotes: 0
Reputation: 223257
The reason you are getting this behaviour is that your one of the delimeter </pre>
happens to exist at the end of the string.
You may see: string.Split - MSDN
...a delimiter is found at the beginning or end of this instance, the corresponding array element contains Empty
To overcome this:
Use StringSplitOptions.RemoveEmptyEntries
instead of StringSplitOptions.None
StringSplitOptions.RemoveEmptyEntries - MSDN
The return value does not include array elements that contain an empty string
var split = content
.Split(new[] { "<pre>", "</pre>" }, StringSplitOptions.RemoveEmptyEntries);
Upvotes: 1
Reputation: 1062780
The "why" is simply: the input (if you don't remove empty entries) will always "split" at any occurrence of the separator(s), so if the separator(s) appear n
times in the string, then the array will be n+1
long. In particular, this essentially lets you know where they occurred in the original string (although when using multiple separators, it doesn't let you know which appeared where).
For example, with a simple example (csv without any escaping etc):
string[] arr = input.Split(','); // even if something like "a,b,c,d,"
// which allows...
int numberOfCommas = arr.Length - 1;
string original = string.Join(",", arr);
The fix is, as already mentioned, to use RemoveEmptyEntries
.
Upvotes: 5
Reputation: 75
Mailou, instead of giving 'StringSplitOptions.None' try 'StringSplitOptions.RemoveEmptyEntries'. It removes the the empty lines.
Upvotes: 1
Reputation: 16698
The split string[] values not include any empty string by using StringSplitOptions.RemoveEmptyEntries
var split = content
.Split(new[] { "<pre>", "</pre>" }, StringSplitOptions.RemoveEmptyEntries);
Reference: StringSplitOptions Enumeration
Upvotes: 0
Reputation: 5801
You also need to specify the
StringSplitOptions.RemoveEmptyEntries enumerator.
Upvotes: 0
Reputation: 75306
Use StringSplitOptions.RemoveEmptyEntries
instead to remove empty string in list
var split = content
.Split(new[] { "<pre>", "</pre>" }, StringSplitOptions.RemoveEmptyEntries);
Upvotes: 2