delete an element from string

53 ビュー (過去 30 日間)
Mahdi Hayati
Mahdi Hayati 2022 年 5 月 21 日
回答済み: ajay kumar 2022 年 7 月 6 日
hi
I have this string:
str = ["a" "b" "c"]
which gives:
"a" "b" "c"
how can I have this new string with the previouse one:
new_str = "a" "c"
in other words, I want to delete "b" completely.
I have tried erase but with that I will have:
"a" "" "c"
thanks in advance.

採用された回答

DGM
DGM 2022 年 5 月 21 日
How do you intend to identify the thing you want to delete? Do you simply want to delete the second string in the array?
str = ["a" "b" "c"];
str(2) = []
str = 1×2 string array
"a" "c"
Or do you want to delete all (or the first) instance of the string "b" in the array?
str = ["a" "b" "c"];
idx = strcmp(str,"b");
str(idx) = []
str = 1×2 string array
"a" "c"
  2 件のコメント
Mahdi Hayati
Mahdi Hayati 2022 年 5 月 21 日
Thank you DGM for both.
per isakson
per isakson 2022 年 5 月 21 日
or
str = ["a" "b" "c"];
str(str=="b") = []
str = 1×2 string array
"a" "c"

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

その他の回答 (1 件)

ajay kumar
ajay kumar 2022 年 7 月 6 日
str = ["a" "b" "c"];
y= find(contains(str,"b"))
y = 2
str(y) = []
str = 1×2 string array
"a" "c"

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by