フィルターのクリア

How can I split string based on a sting array?

4 ビュー (過去 30 日間)
Stergios Verros
Stergios Verros 2022 年 9 月 14 日
編集済み: Stergios Verros 2022 年 10 月 26 日
Hi all,
I have two arrays, say:
first = ["alpha" "beta"];
second = ["{" "}"];
and I want to create a function which receives a string and splits the string in different string arrays(or cell). Each array(or cell) should contain either a single member of one of the two arrays or containing a string that is not a member of the arrays (without including the blank space). Ex:
Input string:
"alpha{ beta} new {} new2} "
Output string:
"alpha" "{" "beta" "new" "{" "}" "new2" "}"
I tried
[matches, non_matches] = strsplit("alpha{ beta} new {} new2}",[first second])
but first of all the outputs are seperated in matches and non_matches and second, the non_matches contain strings that are members of both arrays.
Hope that was clear!
Bests,
Stergios

採用された回答

Stephen23
Stephen23 2022 年 9 月 14 日
編集済み: Stephen23 2022 年 9 月 14 日
S = "alpha{ beta} new {} new2}";
T = ["alpha","beta", "{","}"];
[X,Y] = strsplit(S,T, 'CollapseDelimiters',false);
X = strtrim(X); % you forgot to mention, that you also want to remove whitespace
X(2,:) = [Y,""];
X(strlength(X)==0) = []
X = 1×9 string array
"alpha" "{" "beta" "}" "new" "{" "}" "new2" "}"
  1 件のコメント
Stergios Verros
Stergios Verros 2022 年 9 月 14 日
Perfect Stephen23. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by