Wrong result when Split a string when the given characters are not found

string split c#
c# split string by comma
c# split string by space
string split php
string split java
c# split string into characters
c# split cannot convert string to char
c# split string by length

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();

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

Splits a string into substrings based on the characters in an array. The Split method is not always the best way to break a delimited string into substrings. Value); // The example displays the following output: // 1: This is captured // text. and two delimiters are adjacent or a delimiter is found at the beginning or end of this  If separator contains multiple characters, that entire character sequence must be found in order to split. If separator is omitted or does not occur in str, the returned array contains one element consisting of the entire string. If separator appears at the beginning (or end) of the string, it still has the effect of splitting. The result is an

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

split — Split string into array by regular expression. Warning Split a string into smaller chunks; wordwrap() - Wraps a string to a given number of characters. The matched separator characters are not returned in the result. If no separator match is found then the entire text string is returned as a single result. Use the Concat function to recombine the string without the separators. Use the MatchAll function to split a string using a regular expression.

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.

func split(separator: Character, maxSplits: Int = Int.max, If false , an empty subsequence is returned in the result for each consecutive pair of Elements that are used to split the collection are not returned as part of any subsequence. of the collection, in order, that don't contain elements satisfying the given predicate. Splits the string in the Series/Index from the beginning, at the specified delimiter string. Equivalent to str.split (). Parameters. patstr, optional. String or regular expression to split on. If not specified, split on whitespace. nint, default -1 (all) Limit number of splits in output.

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

The default used by the normalize-unicode() function is NFC (normalization form C), Normalization forms C and KC replace decomposed characters by composed and one of the concerns is that it is also a security risk—using the “​wrong” a non-spacing cedilla that follows it may not produce a very meaningful result,  Notes to Callers. In the .NET Framework 3.5 and earlier versions, if the Split (Char []) method is passed a separator that is null or contains no characters, the method uses a slightly different set of characters to split the string than the Trim (Char []) method does to trim the string.

Split<SeparatorRegex=False>( String , Delimiter, Index ) If the delimiter character is not found in the string of characters, one of two resolutions can occur:​  Hi everyone, in this Python Split String By Character tutorial, we will learn about how to split a string in python. Splitting string means breaking a given string into list of strings. Splitting string is a very common operation, especially in text based environment like – World Wide Web or operating in a text file.

Numbers are created by numeric literals or as the result of built-in functions and operators. Return True if the float instance is finite with integral value, and False otherwise: If the separator is not found, return a 3-tuple containing the string itself, If maxsplit is given, at most maxsplit splits are done, the rightmost ones. Definition and Syntax of VBA Split Function: Split can be defined as a function that can split a text string into an array, by making use of a delimiter character. It returns a zero-based, one-dimensional array holding the parts of the original text string. The Syntax of VBA Split Statement is as follows: Split (text_string, delimiter, limit

In this case, leading whitespace is not included in the resulting string. to left ond right of the string until it is at least size of len, then appends the result to the given IO. Returns the index of search in the string, or nil if the string is not present. #split(separator : Char, limit = nil, *, remove_empty = false, &block : String -> _). split () method returns a list of strings after breaking the given string by the specified separator. separator : The is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator. maxsplit : It is a number, which tells us to split the string into maximum of provided number of times.

Comments
  • "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
  • You get wrong understanding of Split, if the delimeter(++) is not found, it returns the whole string as result.
  • Btw, what do you mean by "a Count did not work"?
  • 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.
  • @maccettura It wouldn't. The original (before my edit) had '++' which would of course not work either.
  • 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.