Sign in to follow this  
clyde

Pointless questions about computer programming

Recommended Posts

I don't need to know this, but I want to know and I haven't been able to find it online.

In C#, why doesn't the string.Split() method need to know the length of the array it's creating?

So for instance:

String[] exampleArray = sampleSentence.Split(new Char[]{':'});

I get why the Char[] doesn't need to know the length, because it is being instantiated with the contents in the brackets. But the String[] doesn't know how many strings it'll end up with; doesn't seem fair.

Just wondering.

Share this post


Link to post
Share on other sites

In that particular case, and in general concerning methods that return collections, the length of the collection will be determined by the operation.  This allows you to pass in a string of arbitrary length with an arbitrary number of parameters and always get the correct result.  The difference between that and creating an array manually is that the string.Split() method will handle the array's creation and return the result, but within the method the length of the array will be determined by the split algorithm.  

 

Also a neat trick with the params keyword in C# is that you don't actually have to create an array to pass in as the argument.  Instead you can just list all the chars you want to be used to split the string separated by commas.  This will be incredibly useful once you get around to writing editor scripts in Unity.

 

 

string[] exampleArray = sampleSentence.Split(',', '/t', 'c');

Share this post


Link to post
Share on other sites

How can I find out what the string.Split() algorithm is? I want to see how it is managing to make an array without initializing its length.

Edit: I have a hypothesis. I bet it is doing a foreach loop through the string and counting how many delimiters are in the string. The array would then be initialized with a size of (number of delimiters in string). If the string starts with or ends with a delimiter, it would subtract an additional 1 for either.

Share this post


Link to post
Share on other sites

You could create a List, Add parts of the string to the List, then use ToArray on the List(?).

Share this post


Link to post
Share on other sites

If you're really interested how the Split method is implemented you can dive into Mono sources, you can even browse them on github, the string implementation is here https://github.com/mono/mono/blob/master/mcs/class/corlib/System/String.cs

 

There are several Split methods but they all seem to call SplitByCharacters at some point which is the method implementing the core algorithm.

 

I didn't study it in detail but it seems it scans the input string for separators and stores their positions in an array. When the position array is full, it calls Array.Resize on it, which doubles its size. After this process is finished it's clear how many substrings the output should have so the output array is created and filled with strings using the split position array.

 

Note this is a System library code so it's written with performance in mind, hence the unsafe code, the use of array instead of list etc. If I had to write similar function myself and I wasn't terribly concerned about the maximum performance possible I would probably use the List + ToArray approach monkehhh mentioned.

Share this post


Link to post
Share on other sites

Are shaders procedurally generated, animated textures?

I'm curious about making a material that has cellular automata doing it's thing on a mesh during playtime. Just curious though, there is a reason I put the question in this thread.

Share this post


Link to post
Share on other sites

Shaders are pieces of code that modify how to render a pixel or a vertex, usually based on their own or neighbour's properties.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this