Can you use replace to remove all characters that aren't letters from a string?
52 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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.
0 个评论
更多回答(1 个)
Shubham Gupta
2019-10-27
编辑:Shubham Gupta
2019-10-27
Try
str = 'I; love123 you-+';
str_new = regexprep(str,'[^a-zA-Z\s]','');
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!