checking character by for loop
이전 댓글 표시
Hi guys,
I want to do that;
script = 'hi I am jo*hn' or ' hi I am jo3hn' (for=5per group)
(I have done for perfect string without noncharecter but I have to write version of that non-charecter entried. Program has to stop for loop when program see noncharacter.)
output=
Hiiam
Jo---
text='hi I am jo*hn'
for control=1:length(text1)
if text1(control) == '[^a-z]'
text1=text1(1:(control-1));
end
end
You do not have to edit printing section. I need only restrict text1 until program see a noncharacter.
댓글 수: 5
Geoff Hayes
2020년 4월 15일
Ugur - what is the intent of this code?
if text1(control) == '[^a-z]'
text1=text1(1:(control-1));
end
Why reset text1 to some subset of characters? I guess what I would like to know is what output are you looking for? Do you need to use a for loop? As for your condition, perhaps you are trying to exit if the character is not one of a-z (or A-Z). If that is the case then try,
for control=1:length(text1)
if ~isempty(regexpi(text1(control),'[^a-z]'))
text1=text1(1:(control-1));
break;
end
end
which "works" though it will exit/break on space characters too. To allow space characters just change your expression to '[^a-z\s]'.
Ugur Sahin
2020년 4월 15일
Geoff Hayes
2020년 4월 15일
You had started using '[^a-z]' so I assumed that you were trying to use a regexp or regexpi. Is this not the case?
Ugur Sahin
2020년 4월 15일
Walter Roberson
2020년 4월 15일
~ismember(text1(control), list_of_permitted_characters)
답변 (1개)
Swaroopa
2022년 8월 17일
편집: Walter Roberson
2022년 8월 17일
You can use isLetter(A) function in the MATLAB.
text1='hi I am jo*hn'
for control=1:length(text1)
if isletter(text1(control))==0
if text1(control)~=' '
text=text1(1:(control-1));
break;
end
end
end
text1=text
Refer to the below documentation for more information:
Hope this helps.
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!