[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

like operator?

Posted on 2006-03-27
10
Medium Priority
?
3,103 Views
Last Modified: 2010-08-05
Is their such a thing as a like in javascript? I have a string and i need to find out if it is like another string.
0
Comment
Question by:willa666
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 16300822
Try this

<script>
var str1, str2, str3;

str1 = "Hello World";
str2 = "Hello";
str3 = "Not in";

alert(str1.indexOf(str2));
alert(str1.indexOf(str3));
</script>
0
 
LVL 33

Expert Comment

by:hongjun
ID: 16300835
indexOf will give you -1 like in the situation above where str3 is not in str1
indexOf will give you >= 0 (position start from 0) where str2 is first found in str1.
0
 
LVL 1

Author Comment

by:willa666
ID: 16301031
coolio!

thats works a treat! what if i want the end to match?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:hongjun
ID: 16301203
Don't understand
0
 
LVL 1

Author Comment

by:willa666
ID: 16301520
well instead of it getting the indexof a string it looks for the end of the string to match.

u c
0
 
LVL 11

Expert Comment

by:mvan01
ID: 16303903
indexOf will find it anywhere.

Looking for hello in all of these:

<script>
var str1, str2, str3;

str1 = "Hello";
str2 = "Hello World";
str3 = "Not in here, it isn't";
str4 = "Well, you didn't even say Hello when I saw you yesterday";

alert(str2.indexOf(str1));
alert(str3.indexOf(str1));
alert(str4.indexOf(str1));
</script>

These will return 0, -1, 26 .  Any value>=0 indicates a match.

You should probably turn all strings to lowercase before comparing, because you wouldn't find a match for "Hello" in "You say goodbye and I say hello." because of the difference in capitalization.

Peace and joy to you.
mvan
0
 
LVL 11

Accepted Solution

by:
mvan01 earned 1000 total points
ID: 16322918
This shows how to do the lowercase properly...  Peace and joy.  mvan

<script>
var str1, str2, str3, str4, str5;

str1 = "Hello";
str2 = "Hello World";
str3 = "Not in here, it isn't";
str4 = "Well, you didn't even say Hello when I saw you yesterday";
str5 = "You say goodbye and I say hello";

document.write(str1+((str2.indexOf(str1)>-1)?' is':' is NOT')+' like: "'+str2+'"<br>');
document.write(str1+((str3.indexOf(str1)>-1)?' is':' is NOT')+' like: "'+str3+'"<br>');
document.write(str1+((str4.indexOf(str1)>-1)?' is':' is NOT')+' like: "'+str4+'"<br>');
document.write('<br>');
document.write(str1+((str5.indexOf(str1)>-1)?' is':' is NOT')+' like: "'+str5+'"<br>');
document.write('<br>');
document.write(' * However, converting all to lowercase for the comparison:<br>');
document.write(str1+((str5.toLowerCase().indexOf(str1.toLowerCase())>-1)?' is':' is NOT')+' like: "'+str5+'"<br>');
document.write('<br>');
document.write(' * test is:  (lookInStr.toLowerCase().indexOf(findMeStr.toLowerCase())>-1)<br>');
document.write('             you supply var lookInStr and var findMeStr<br>');
</script>
0
 
LVL 2

Assisted Solution

by:ppedregal
ppedregal earned 1000 total points
ID: 16425286
You can also use regular expressions to test strings

var str = "Hello world!";
// test if contains "Ello" case sensitive
var rexp1 = /ello/g;
rexp1.test(str);  // returns false
// test if contains "Ello" not case sensitive
var rexp2 = /ello/ig;
rexp2.test(str); // returns true
// test if ends with "world"
var rexp3 = /world$/;
rexp3.test(str);

Some good references about regular expressions:
http://www.devguru.com/Technologies/ecmascript/quickref/regexp.html
http://www.devguru.com/Technologies/ecmascript/quickref/regexp_special_characters.html
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question