Is it possible to compare 2 strings only some of the same alphabet not the whole?
1 次查看(过去 30 天)
显示 更早的评论
If I have 2 variables such as a = ' term'; and b ='computer'; and ter is the same.
Is it possible to compare and turn the true ? I want to compare like query data with 'LIKE' command.
Thank you. Sorry for not good in English.
3 个评论
Image Analyst
2015-1-7
It's not just ter that is a substring that is present, there are lots of substrings present. Do you also want it to return starting locations of all places where t, e, r, and te occur?
采纳的回答
dpb
2015-1-7
编辑:dpb
2015-1-7
Base Matlab doesn't have the pattern matching with the general substring search for any substring within the two without specifically specifying the substring length and working thru the two, no.
There's always the File Exchange (see link at top of page) for user-contributed submissions that's worth a look.
Indeed, a look-see found--
I didn't like the name much so renamed the m-file to LCSTR.M. If it were mine I'd also rearrange the outputs so the default, first output would be the string itself, but I suppose the author had other uses in mind when chose the interface.
Anyway, for your example,
>> [o1,o2,o3]=lcstr(a,b)
o1 =
0.6000
o2 =
3
o3 =
ter
>>
NB: This function returns only a single matching string of the longest number of characters between the two input strings. If there are more than one of the same length or others of shorter length, those won't be found.
I didn't see a more generic function within File Exchange submittals in a fairly quick perusal; there may be something else if this isn't precisely what you want. Or, of course, use this as a starting point to "roll your own".
PS: I'd think there would be a way with regexp to do this but I'm so feeble with writing regular expressions I'm not even going to try...
ADDENDUM
For the answer to the specific question asked, something like
lcstr(a,b)~=0;
will return a logical, but...
It appears this routine is somewhat buggy; I tried
lcstr(fliplr(a),b)
just to see what it would do for a failed match and discovered it thinks 'mt' "matches". I didn't dig into seeing why, precisely, but looks like you'll need to do some serious testing rather than just dropping this into your application.
2 个评论
dpb
2015-1-8
I'd caution you to look at the above ADDENDUM results carefully before relying too heavily on the routine as it stands...
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!