MATLAB: How to check if string contains some special character

string check

Dear all,
I have a question that the best way to check if string contains:
  1. alphanumeric characters and underscore only (ex: stringabc: return 1, string abc: return 0)
  2. underscore at the beginning or the end (ex: _stringabc, stringabc_ : return 0)
  3. consecutive underscores (ex: string__abc : return 0)
  4. numbers at the beginning (ex: 012string_abc : return 0)
Thank you so much

Best Answer

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