你好,
是否有任何內置函數可以將字符串轉換為列表? 例如,我有一個字符串“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"}