How to check if string contains some special character?
조회 수: 57 (최근 30일)
이전 댓글 표시
Dear all,
I have a question that the best way to check if string contains:
- alphanumeric characters and underscore only (ex: stringabc: return 1, string abc: return 0)
- underscore at the beginning or the end (ex: _stringabc, stringabc_ : return 0)
- consecutive underscores (ex: string__abc : return 0)
- numbers at the beginning (ex: 012string_abc : return 0)
Thank you so much
댓글 수: 0
채택된 답변
Image Analyst
2020년 12월 15일
For #1 you could use isstrprop().
For #2, see startsWith(str, '_') and endsWith(str, '_')
For #3, you can use find():
indexes = strfind(str, '__');
For #4 you could do something like
if str(1) >= '0' && str(1) <= '9'
% str starts with a numerical digit.
else
% str does not start with a numerical digit.
end
댓글 수: 2
Image Analyst
2020년 12월 15일
Sorry, I don't know that language. But you can always (I think) use logical comparison like
itsAChar = str(1) >= 'a && str(1) <= 'z'
where you just put in the lowest unicode character and highest unicode character instead of a and z.
추가 답변 (0개)
참고 항목
카테고리
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!