Filter löschen
Filter löschen

How can I split string based on a sting array?

4 Ansichten (letzte 30 Tage)
Stergios Verros
Stergios Verros am 14 Sep. 2022
Bearbeitet: Stergios Verros am 26 Okt. 2022
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

Akzeptierte Antwort

Stephen23
Stephen23 am 14 Sep. 2022
Bearbeitet: Stephen23 am 14 Sep. 2022
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" "}"

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by