How to compare strings within a loop
19 次查看(过去 30 天)
显示 更早的评论
I am trying to create a program in which I want to compare my saved data which I have stored in cell with respect to the input given by the user. Since I have lot of data I dont want to compare one with each other manually, but I want to use loops command. Please point out my mistakes in below
clear all; close all; clc;
str{1} = 'http://vacationet.com/resort.php?id=22';
str{2} = 'http://jokusoftware.cz/file.php?id=icqj3';
str{3} = 'http://www.nichegardens.com/catalog/item.php?id=19114';
a0=input('Paste the http link to check its vulunerability ', 's');
n=1
for i=str{n};
if (strcmpi(a0,i))
sprintf('%s is vulunerable',a0);
else
n=n+1;
continue;
end
end
sprintf('%s is non vulunerable',a0)
采纳的回答
Sindar
2020-4-27
编辑:Sindar
2020-4-27
No need to loop, strcmpi will compare a string to all strings in an array. If you don't care which string matches, use "any":
str{1} = 'url1';
str{2} = 'url2';
str{3} = 'url3';
a0=input('Paste the http link to check its vulunerability ', 's');
if any(strcmpi(a0,str))
sprintf('%s is vulunerable',a0);
else
sprintf('%s is non vulunerable',a0)
end
(Answers doesn't like links, thinks they mean spam)
1 个评论
Sindar
2020-4-27
If you were to do it in a loop, make sure you're looping over the strings, not the characters in them (as you appear to be):
str{1} = 'url1';
str{2} = 'url2';
str{3} = 'url3';
a0=input('Paste the http link to check its vulunerability ', 's');
isvulnerable = false;
for ind=1:length(str)
if strcmpi(a0,str{ind})
sprintf('%s is vulunerable',a0);
% if you find a match, end the loop and record
isvulnerable=true;
break
end
end
% after checking all of them, print if no matches were found
if ~isvulnerable
sprintf('%s is non vulunerable',a0)
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!