1

When I tried to split a string value of some text here with ++. I expected the result to be an empty list. Since the ++ is not found in the string some text here, the result of a Count on the List should be 0.

However, the result I get is 1 (when I Count on the List).

How am I able to determine if the string has no ++ in it ? (a Count did not work)

List<string> l = value.Split("++").ToList();
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Illep
  • 16,375
  • 46
  • 171
  • 302
  • 4
    *"I expected the result to be an empty list."* Well then your expectation is wrong. If you need to check if the string contains `"++"` simply check with [String.Contains](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netframework-4.7.2) – Manfred Radlwimmer Oct 09 '18 at 18:40
  • 4
    You get wrong understanding of Split, if the delimeter(++) is not found, it returns the whole string as result. – mukesh kudi Oct 09 '18 at 18:40
  • Btw, what do you mean by *"a Count did not work"*? – Manfred Radlwimmer Oct 09 '18 at 18:41
  • Use contains to determine if the charact(s) are in the string. If the character are not in the string you will get an array of size one with the same sting that you started with. – jdweng Oct 09 '18 at 18:41
  • Would this even compile? I am not aware of any overload to `Split()` that accepts a single string – maccettura Oct 09 '18 at 18:42
  • 1
    @maccettura It wouldn't. The original (before my edit) had `'++'` which would of course not work either. – Manfred Radlwimmer Oct 09 '18 at 18:43
  • Also, if you look at [the docs](https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.7.2) for the _actual_ split method overloads you will see this: `"If this instance does not contain any of the strings in separator, or the count parameter is 1, the returned array consists of a single element that contains this instance."` – maccettura Oct 09 '18 at 18:44
  • Possible duplicate of [How can I check if a string exists in another string](https://stackoverflow.com/questions/5848337/how-can-i-check-if-a-string-exists-in-another-string) – Manfred Radlwimmer Oct 09 '18 at 18:47

4 Answers4

3

The observed behavior is by design. If delimiter is not found a collection with a single item is returned. As documentation states:

If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance.

If you want to check if delimiter exists you can use .Contains("++") or .IndexOf("++") != -1

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • I'd definitely avoid the count option. If you later decide to use `StringSplitOptions.RemoveEmptyEntries` then a string like "stuff++" will incorrectly be identified as not containing ++ when it clearly does. – Jonathon Chase Oct 09 '18 at 18:50
1

By default, if no matches are found it returns the string in a array of size one.

How am I able to determine if the string has no ++ in it ?

if (value.Contains("++"))

edit: wow a bunch of answers already while i was writing this. :D

Bojo
  • 313
  • 4
  • 7
0

As @Gilad and others have pointed out, this is indeed the expected output. If a string does not contain the split value, the entire string is returned as the first item in the list.

If you plan on using this split value later, you can still use the .Split() method to determine if your split string is contained within the string, by simply checking if the count is equal to 1:

List<string> l = value.Split(new[] {"++"}).ToList();

if (l.Count == 1) {
    //++ was not found in the string
} else {
    //++ was found in the string (l.Count-1) times
}

Note of caution: It's less efficient to split a string, and allocate an array, than simply just checking with a method such as .Contains(). Use the above solution, if you may actually use the above split items later in the code.

Blue
  • 22,608
  • 7
  • 62
  • 92
0

If there is no "++" in the string, you get back the original string. If there are n "++"'s in the string, you get n+1 splits returned. You code is fine except that it needs an array passed:

var l = value.Split(new string[] {"++"}, StringSplitOptions.None).ToList();

So when l.Count() == 1 then there is no "++" in the string

adjan
  • 13,371
  • 2
  • 31
  • 48