Highlighted

Community Trekker

Joined:

Feb 9, 2017

## Convert a string to a list

Hello,

Is there any builtin function to convert a string to a list? For example, I have a string "abcd1234cdef5678" and I want it to be converted to {"abcd", "1234", "cdef, "5678""}.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

Super User

Joined:

Jun 22, 2012

Solution

## Re: Covert a string to a list

I am not aware of such a function, but you could use this code or make it a function

`Names Default To Here( 1 );TheString = "abcd1234cdef5678";TheList = {};i = 1;While( Substr( TheString, i, 4 ) != "", Insert Into( TheList, Substr( TheString, i, 4 ) ); i = i + 4;);`

Or here it is as a function

``````SeparateString = Function( {TheString},
TheList = {};
i = 1;
While( Substr( TheString, i, 4 ) != "",
Insert Into( TheList, Substr( TheString, i, 4 ) );
i = i + 4;
);
TheList;
);

MyList = SeparateString("abcd1234cdef5678");``````
Jim
4 REPLIES

Super User

Joined:

Jun 22, 2012

Solution

## Re: Covert a string to a list

I am not aware of such a function, but you could use this code or make it a function

`Names Default To Here( 1 );TheString = "abcd1234cdef5678";TheList = {};i = 1;While( Substr( TheString, i, 4 ) != "", Insert Into( TheList, Substr( TheString, i, 4 ) ); i = i + 4;);`

Or here it is as a function

``````SeparateString = Function( {TheString},
TheList = {};
i = 1;
While( Substr( TheString, i, 4 ) != "",
Insert Into( TheList, Substr( TheString, i, 4 ) );
i = i + 4;
);
TheList;
);

MyList = SeparateString("abcd1234cdef5678");``````
Jim

Community Trekker

Joined:

Feb 9, 2017

## Re: Covert a string to a list

Thanks a lot Jim!

Community Manager

Joined:

Jun 23, 2011

## Re: Covert a string to a list

Take a look at the Words()  function. If you can define delimiters which separate the elements of the list then it will work for you.

-Jeff

Staff

Joined:

Mar 21, 2013

## Re: Covert a string to a list

Another technique:

``````parts = {};
rc = Pat Match( "1234567890abcdefghijklmnopqrstuv", Pat Repeat( Pat Len( 4 ) >> parts[N Items( parts ) + 1] ) );
Show( parts );
``````

parts = {"1234", "5678", "90ab", "cdef", "ghij", "klmn", "opqr", "stuv"};

Jim's approach is better for short strings because the next person that maintains the JSL will understand it. For strings that are 10K or so, the pattern matching will be noticably faster.

The pattern match (red curve) avoids reprocessing the string

The is the JSL to make the graph...

``````dt = New Table( "Untitled",
New Column( "n", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "forloopTime", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
New Column( "patternTime", Numeric, "Continuous", Format( "Best", 12 ), Set Selected, Set Values( [] ) )
);

For( nstr = 1, nstr < 10000, nstr *= 2,
dt:n = nstr * 4; // four substrings in the repeat...
TheString = Repeat( "abcd1234cdef5678", nstr );
start = Tick Seconds();
TheList = {};
i = 1;
While( Substr( TheString, i, 4 ) != "",
Insert Into( TheList, Substr( TheString, i, 4 ) );
i = i + 4;
);
stop = Tick Seconds();
dt:forloopTime = (stop - start);

start = Tick Seconds();
parts = {};
rc = Pat Match( theString, Pat Repeat( Pat Len( 4 ) >> parts[N Items( parts ) + 1] ) );
stop = Tick Seconds();
dt:patternTime = (stop - start);
Wait( 0 );
);

dt << Graph Builder(
Size( 764, 624 ),
Show Control Panel( 0 ),
Legend Position( "Inside Left" ),
Variables( X( :n ), Y( :forloopTime ), Y( :patternTime, Position( 1 ) ) ),
Elements( Points( X, Y( 1 ), Y( 2 ), Legend( 9 ) ), Smoother( X, Y( 1 ), Y( 2 ), Legend( 10 ) ) ),
SendToReport(
Dispatch( {}, "n", ScaleBox, {Min( -1453.48837209302 )} ),
Dispatch( {}, "forloopTime", ScaleBox, {Format( "Best", 12 ), Min( -0.949803149606299 )} ),
Dispatch( {}, "graph title", TextEditBox, {Set Text( "Compare algorithms" )} ),
Dispatch( {}, "X title", TextEditBox, {Set Text( "n patterns " )} ),
Dispatch( {}, "Y title", TextEditBox, {Set Text( "Seconds" )} ),
Dispatch( {}, "400", LegendBox, {Set Title( "" )} )
)
);``````

The reason the for loop example slows down on large strings: the substr function must find the location of each substring, starting from the beginning. The pattern match moves through the string once

Craige