[ZScript] String.Split method

Moderator: GZDoom Developers

Post Reply
User avatar
phantombeta
Posts: 2084
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

[ZScript] String.Split method

Post by phantombeta »

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 ();
}
ZippeyKeys12
Posts: 111
Joined: Wed Jun 15, 2016 2:49 pm

Re: [ZScript] String.Split method

Post by ZippeyKeys12 »

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. :?
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: [ZScript] String.Split method

Post by Gutawer »

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.
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

Re: [ZScript] String.Split method

Post by argv »

There is a request for reading from arbitrary lumps. This would be useful for parsing them.
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: [ZScript] String.Split method

Post by Gutawer »

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.
_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: [ZScript] String.Split method

Post by _mental_ »

Added in c4865d2.
User avatar
Major Cooke
Posts: 8170
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: [ZScript] String.Split method

Post by Major Cooke »

Can I get a simpler example of how this works? I'll use that to put on the wiki.
_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: [ZScript] String.Split method

Post by _mental_ »

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.
User avatar
Major Cooke
Posts: 8170
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: [ZScript] String.Split method

Post by Major Cooke »

Empty tokens being spaces?
User avatar
Rachael
Posts: 13532
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her
Contact:

Re: [ZScript] String.Split method

Post by Rachael »

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.
_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: [ZScript] String.Split method

Post by _mental_ »

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.
Post Reply

Return to “Closed Feature Suggestions [GZDoom]”