Can you use replace to remove all characters that aren't letters from a string?

116 ビュー (過去 30 日間)
Zach Adams
Zach Adams 2019 年 10 月 27 日
回答済み: Steven Lord 2019 年 10 月 28 日
I was wondering if I can use replace(str, ~['A':'Z' 'a':'z'], ' ') to replace all values that are not letters with an empty character. For example, if the string was 'I; love123 you-+'
I would need it to make a new string that is 'I love you'. Additionally, if this is not possible is there another way of doing this?

採用された回答

Steven Lord
Steven Lord 2019 年 10 月 28 日
You can use isstrprop to create a mask for various categories of characters then use that mask to extract just all letters, or just all numbers, etc.

その他の回答 (1 件)

Shubham Gupta
Shubham Gupta 2019 年 10 月 27 日
編集済み: Shubham Gupta 2019 年 10 月 27 日
Try
str = 'I; love123 you-+';
str_new = regexprep(str,'[^a-zA-Z\s]','');
  1 件のコメント
Zach Adams
Zach Adams 2019 年 10 月 28 日
That worked! It will not let me accept the answer at the moment so I will keep trying. Thanks.

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

カテゴリ

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