How to find first letter of my string is alphabetic??

31 ビュー (過去 30 日間)
shivaraj c
shivaraj c 2018 年 12 月 5 日
コメント済み: Kevin Chng 2018 年 12 月 5 日
I have string like "20inputsignal" in this if my string starts with number i need to give some error message how to do this?
expected strings are "in1"--->ok "in20signal1"---->ok but i dont need string starting with number.
for "in1" and "in20signal1" im using reg expression to filter out number both are fine but user should insert those two types only not the "20inputsignal" like string. how to give error condition for this

回答 (2 件)

Adam
Adam 2018 年 12 月 5 日
This works, I think, but I'm sure there are far neater approaches that people will come up with!!
str = "20inputsignal";
ch = char( str );
valid = isnan( str2double( ch(1) ) ) || ~isreal( str2double( ch(1) ) )
The fact that 'i' is interpreted as 0 + 1i when converted to a double adds more complexity than I at first thought!

Kevin Chng
Kevin Chng 2018 年 12 月 5 日
let say
a = "in1"
check = all(ismember(a(1), '0123456789'));
if check is 1, then it is not okay, else vice versa.
  4 件のコメント
Adam
Adam 2018 年 12 月 5 日
You need to convert the string to a char if you are using a(1) as a string like that is a scalar representing the whole string rather than a char array where you can index each character.
Kevin Chng
Kevin Chng 2018 年 12 月 5 日
a = "in1"
b = char(a)
check = all(ismember(b(1), '0123456789'));

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

カテゴリ

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