[Added] [ZScript] String.Split method

Moderator: Developers

[ZScript] String.Split method

Postby phantombeta » Sat Oct 07, 2017 6:09 am

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

Possible prototypes:
Code: Select allExpand view
String[] Split (String separator);
-or-
Array<String> Split (String separator);


Example:
Code: Select allExpand view
// 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 ();
}
User avatar
phantombeta
In the meadow of sinful thoughts, every flower's a perfect one
 
Joined: 02 May 2013
Location: The United Soviet Socialist Dictatorship of Hueland
Discord: phantombeta#2461
Twitch ID: doom2fan

Re: [ZScript] String.Split method

Postby ZippeyKeys12 » Wed Oct 25, 2017 8: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. :?
ZippeyKeys12
 
Joined: 15 Jun 2016

Re: [ZScript] String.Split method

Postby Gutawer » Thu Oct 26, 2017 12:58 pm

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.
User avatar
Gutawer
User Accounts Assistant
 
Joined: 16 Apr 2016
Discord: Gutawer#3431

Re: [ZScript] String.Split method

Postby argv » Fri Oct 27, 2017 6:06 pm

There is a request for reading from arbitrary lumps. This would be useful for parsing them.
argv
 
Joined: 30 Aug 2016

Re: [ZScript] String.Split method

Postby Gutawer » Fri Oct 27, 2017 6: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.
User avatar
Gutawer
User Accounts Assistant
 
Joined: 16 Apr 2016
Discord: Gutawer#3431

Re: [ZScript] String.Split method

Postby _mental_ » Sat Oct 28, 2017 4:23 am

Added in c4865d2.
_mental_
 
 
 
Joined: 07 Aug 2011

Re: [ZScript] String.Split method

Postby Major Cooke » Sat Oct 28, 2017 12:46 pm

Can I get a simpler example of how this works? I'll use that to put on the wiki.
User avatar
Major Cooke
The road to Hell is paved in the carrion she leaves behind.
 
Joined: 28 Jan 2007
Discord: Major Cooke#0846

Re: [ZScript] String.Split method

Postby _mental_ » Sat Oct 28, 2017 2:50 pm

Code: Select allExpand view
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.
_mental_
 
 
 
Joined: 07 Aug 2011

Re: [ZScript] String.Split method

Postby Major Cooke » Sat Oct 28, 2017 3:25 pm

Empty tokens being spaces?
User avatar
Major Cooke
The road to Hell is paved in the carrion she leaves behind.
 
Joined: 28 Jan 2007
Discord: Major Cooke#0846

Re: [ZScript] String.Split method

Postby Rachael » Sat Oct 28, 2017 3: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.
User avatar
Rachael
QZDoom + Webmaster
 
Joined: 13 Jan 2004

Re: [ZScript] String.Split method

Postby _mental_ » Sun Oct 29, 2017 1:23 am

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

Updated example:
Code: Select allExpand view
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.
_mental_
 
 
 
Joined: 07 Aug 2011


Return to Closed Feature Suggestions

Who is online

Users browsing this forum: Google [Bot] and 1 guest