cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
aumair
Level III

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!

Ahmad

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

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

View solution in original post

8 REPLIES 8
txnelson
Super User

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
aumair
Level III

Re: Covert a string to a list

Thanks a lot Jim! 

RA899
Level III

Re: Covert a string to a list

Hi Jim,

 How would you approach this if you have a string, and a comma is where you want to stop separating the string. 
For example, 

converting this " C = "It is, raining, very, hard today" to b = {"It is" , "raining" , "very", "hard today" } 

 

Thanks! 

RA899
Level III

Re: Covert a string to a list

Update: 

I have found an older thread about this from  Jeff_Perkinson. 

This is the link Solved: Using several characters as delimiter for the Words function - JMP User Community 

The solution that worked for me was using this method: 

words(substitute("Test1eeTest2", "ee", "!"),"!")

/*:

{"Test1", "Test2"}


Thanks. 

-Rakan

txnelson
Super User

Re: Covert a string to a list

Use the Words() function to split the string into a List with the elements of the being each of the substrings identified by the specified delimiter, in your case a comma.

names default to here(1);

C = "It is, raining, very, hard today";
b = Words( C, "," );

 

Jim
Jeff_Perkinson
Community Manager Community Manager

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
Craige_Hales
Super User

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 stringThe pattern match (red curve) avoids reprocessing the string

The is the JSL to make the graph...

dt = New Table( "Untitled",
	Add Rows( 0 ),
	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 << addrows( 1 );
	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
bswedlove
Level IV

Re: Covert a string to a list

If converting string to list of single characters (a little different from OP's request but matches the title) then can do:

Items( "hello", "" );

Which returns:

{"h", "e", "l", "l", "o"}