Split method for a string

Forums for specific tips, techniques and example code
User avatar
andy-b
Posts: 24
Joined: Mon Jul 05, 2010 2:00 pm
Location: Dunedin, NZ

Split method for a string

Postby andy-b » Fri Mar 11, 2011 9:40 am

Hello

I was looking in the Jade documentation for a method to split up a string on a particular character.
I think the closest I could find is String.getTokens which returns a StringArray of the individual parts of a string but is split on specific characters.

Ideally I would like the same function but to take in a character (or string) parameter and have the string divided up on that. So for example "a;b;c;d;e".split(';') would produce [[a], , [c], [d], [e]]

I don't mind writing my own function to do this, was just wondering if anyone else has done this before me, or if I missed what I want in the Jade Documentation :)

Cheers
Andy

User avatar
andy-b
Posts: 24
Joined: Mon Jul 05, 2010 2:00 pm
Location: Dunedin, NZ

Re: Split method for a string

Postby andy-b » Fri Mar 11, 2011 11:03 am

Hold the phone...CardSchema has just what I need :D

Code: Select all

cnGetTokens(delimiter : String) : CnStringArray;

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Split method for a string

Postby BeeJay » Fri Mar 11, 2011 12:18 pm

Although it would have been better for that method to take an input array for the results, rather than returning a transient that it created, so that there is less likelihood someone forgets to delete the transient. ;)

bdoolan
Posts: 19
Joined: Fri Aug 14, 2009 7:26 pm

Re: Split method for a string

Postby bdoolan » Sat Mar 12, 2011 5:40 am

Agree with Beejay's comment re creating the transient array and using this as an input parameter. One other comment I would make. StringArrays can often give the "String too long" exception due to having fixed length element strings not being long enough to hold the string. A way round this is to use a PointArray parameter instead of the StringArray parameter and use this to hold the offset and length of each token.

So something like

Code: Select all

split(pDelimiter : String; pOffLens : PointArray input); vars curPos, endPos : Integer; pt : Point; begin curPos := 1; while curPos <= self.length do endPos := self.pos(pDelimiter, curPos); if endPos < 1 then // fallen off end of string endPos := self.length + 1; endif; pt.set(curPos, endPos-curPos); pOffLens.add(pt); // add offset/ length pair to pOffLens array curPos := endPos + pDelimiter.length; endwhile; end;
Remember that, when using the offset/ length to pull the token from the original string, the original string should be assigned to a local variable (cf recent post re Speeding up string concatenation (https://forums.jadeworld.com/viewtopic.php?f=9&t=1705)).

Cheers, Brendan

Paul
Posts: 1
Joined: Thu Mar 17, 2011 9:14 am

Re: Split method for a string

Postby Paul » Thu Mar 17, 2011 9:18 am

You could also try cnUnbust which will take an array io parameter. The array is a CnStringArray, though.

Paul

Rich
Posts: 4
Joined: Tue Jun 23, 2009 8:33 am

Re: Split method for a string

Postby Rich » Thu Mar 17, 2011 10:51 am

Yes, in hindsight the cnGetTokens method should have required an array as input. But when this was written in the mid 1990s, the authors was an OO and JADE newbie himself. Once there was an established user base for CardSchema, it was not possible to change these methods without upsetting existing user code.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 2 guests

cron