Can you use replace to remove all characters that aren't letters from a string?
조회 수: 29 (최근 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에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!