Is it possible to compare 2 strings only some of the same alphabet not the whole?
조회 수: 2 (최근 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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!