你好,
是否有任何内置函数可以将字符串转换为列表? 例如,我有一个字符串“abcd1234cdef5678”,我希望将其转换为{“abcd”,“1234”,“cdef,“5678””}。
谢谢!
艾哈迈德
另一种技术:
parts = {};
rc = Pat Match( "1234567890abcdefghijklmnopqrstuv", Pat Repeat( Pat Len( 4 ) >> parts[N Items( parts ) + 1] ) );
Show( parts );
零件= {“1234”,“5678”,“90ab”,“cdef”,“ghij”,“klmn”,“opqr”,“stuv”};
Jim 的方法更适合短字符串,因为下一个维护 JSL 的人会理解它。 对于 10K 左右的字符串,模式匹配会明显更快。
The pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the stringThe pattern match (red curve) avoids reprocessing the string模式匹配(红色曲线)避免重新处理字符串
这是制作图表的 JSL...
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( "" )} )
)
);
for 循环示例在大字符串上变慢的原因:substr 函数必须找到每个子串,从开始。 模式匹配在字符串中移动一次。
我不知道这样的函数,但您可以使用此代码或将其设为函数
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;
);
</CODE></PRE>
<P>或者这里它是一个函数</P>
<PRE><CODE class=" language-jsl">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");
嗨吉姆,
如果你有一个字符串,而逗号是你想要停止分隔字符串的地方,你会如何处理这个问题。
例如,
将此“ C = “今天下雨了,非常非常难”转换为 b = {“It is” , “raining” , “very”, “今天很难” }
谢谢!
更新:
我发现了一个关于这个问题的旧线程 杰夫·珀金森。
这是已解决的链接:使用多个字符作为单词函数的分隔符 - JMP 用户社区
对我有用的解决方案是使用这种方法:
words(substitute("Test1eeTest2", "ee", "!"),"!")
/*:
{"Test1", "Test2"}
<BR />谢谢。
-朋友
使用 Words() 函数将字符串拆分为列表,其中每个子字符串的元素由指定分隔符(在您的情况下为逗号)标识。
names default to here(1);
C = "It is, raining, very, hard today";
b = Words( C, "," );
如果将字符串转换为单个字符的列表(与 OP 的要求略有不同但与标题相符)则可以执行以下操作:
Items( "hello", "" );
返回:
{"h", "e", "l", "l", "o"}