[ZScript] String.Split method

Post a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: [ZScript] String.Split method

Re: [ZScript] String.Split method

by _mental_ » Sun Oct 29, 2017 12:23 am

Empty tokens are... Surprise! Empty strings. They are not null’s but strings with zero length.

Updated example:

Code: Select all

String str = "one, two, , four";
Array<String> tokens;
str.Split(tokens, ", "); // tokens contains 4 items: "one", "two", "", "four"
By the way Split() doesn’t clear array passed to it.

Re: [ZScript] String.Split method

by Rachael » Sat Oct 28, 2017 2:46 pm

Empty tokens being empty.

An example is "1,,3" - [1]==1, [2]==null, [3]==3

If space removal is needed, you might need a separate function for that.

Re: [ZScript] String.Split method

by Major Cooke » Sat Oct 28, 2017 2:25 pm

Empty tokens being spaces?

Re: [ZScript] String.Split method

by _mental_ » Sat Oct 28, 2017 1:50 pm

Code: Select all

String str = "one,two,three";
Array<String> tokens;
str.Split(tokens, ",");
Empty tokens will be discarded if third optional argument is set to TOK_SKIPEMPTY. By default they will be kept.

Re: [ZScript] String.Split method

by Major Cooke » Sat Oct 28, 2017 11:46 am

Can I get a simpler example of how this works? I'll use that to put on the wiki.

Re: [ZScript] String.Split method

by _mental_ » Sat Oct 28, 2017 3:23 am

Added in c4865d2.

Re: [ZScript] String.Split method

by Gutawer » Fri Oct 27, 2017 5:23 pm

To be completely honest parsing complex stuff that would need to be in a text lump sounds like a terrible use for a split method. Split is useful for very basic stuff like seperating out comma-seperated args, not full on file parsing.

Re: [ZScript] String.Split method

by argv » Fri Oct 27, 2017 5:06 pm

There is a request for reading from arbitrary lumps. This would be useful for parsing them.

Re: [ZScript] String.Split method

by Gutawer » Thu Oct 26, 2017 11:58 am

This is useful for parsing string parameters in a NetworkProcess event handler. Since the arguments are by default all integers, you essentially need to put any desired string arguments in the name of the event handler - you could use , as a seperator, making String.split useful.

Re: [ZScript] String.Split method

by ZippeyKeys12 » Wed Oct 25, 2017 7:42 am

When would one ever use this? Whenever I've used String.split in other languages it has been to parse a file being read but we don't currently have that capability. Just wondering what your use case is. :?

[ZScript] String.Split method

by phantombeta » Sat Oct 07, 2017 5:09 am

A simple method for splitting a string into an array of strings using a separator string.

Possible prototypes:

Code: Select all

String[] Split (String separator);
-or-
Array<String> Split (String separator);
Example:

Code: Select all

// Chooses a random int from a string. Ints are separated by commas (no spaces)
int RandomIntFromString (String input) {
    Array<String> intStrings = input.Split (",");

    return intStrings [random (0, intStrings.Size ())].ToInt ();
}

Top