Resolved Does documentation claim you can split Strings on Strings?

2Flow2

New member
Joined
Apr 6, 2022
Messages
2
Programming Experience
5-10
I don't understand what I am reading wrong...
According to this section of the documentation, Strings should be able to be split using a second String as the seperator. The documentation says:

Split(String, StringSplitOptions)​

Splits a string into substrings that are based on the provided string separator.

C#:
public string[] Split (string? separator, StringSplitOptions options = System.StringSplitOptions.None);

Parameters​

separator String
A string that delimits the substrings in this string.

options StringSplitOptions
A bitwise combination of the enumeration values that specifies whether to trim substrings and include empty substrings.

Returns
String[]
An array whose elements contain the substrings from this instance that are delimited by separator.

However, when I actually attempt this, the code fails and throws an error, claiming that the seperator must be a char or string array, but not a String itself.
Argument 1: Cannot conver from 'string' to 'char'.

In fact, this seems to be backed up by the code internals itself. If I look at System.String, I can find overloads for a variety of seperator arguments, but none of them are for a seperator simply of type String:

System.String definition:
        public String[] Split(char[] separator, int count, StringSplitOptions options);

        public String[] Split(String[] separator, StringSplitOptions options);

        public String[] Split(String[] separator, int count, StringSplitOptions options);

        public String[] Split(char[] separator, int count);

        public String[] Split(params char[] separator);

        public String[] Split(char[] separator, StringSplitOptions options);

The documentation says that the overload accepting a String as a seperator has been present since .NET Core 2.0? So this shouldn't be a brand-new feature to .NET, I don't believe?

So what am I reading incorrectly in this documentation? What does the linked section mean if not that a String can be used as the seperator to .Split() other strings? I'm completely baffled.
 
That is the documentation for .Net 6 (look at Version top left), it has different parameters than .Net Framework.
In .Net Framework you have to use a string array if you want to split by a string, see String.Split Method (System)
 
Solution
Your reply sent me down a learning rabbit hole into what the differences between all of the .NET versions are ultimately ending here. I think I have a pretty good grasp of it now, and I'm using .NET Framework 4.6 for my current project, so your response makes perfect sense. Thank you so much for your reply!
 

Latest posts

Back
Top Bottom