Click to See Complete Forum and Search --> : How to know if a string is contained in another?


arturion
08-17-2003, 03:39 PM
I want if there is a signal to know if a string is contained in other, for example, "teach" is contained in "teachers","teachers" is contained in "teachers", but "tomb" is not contained in "teachers". Does a symbol exists, such as <=, == or something?

AdamGundry
08-17-2003, 03:48 PM
You can use String.indexOf() (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/string.html#1196895), for example:

var str1 = 'teachers';
var str2 = 'teach';

if (str1.indexOf(str2) != -1){
// str2 is in str1
} else {
// str2 is not in str1
}

Adam

pyro
08-17-2003, 04:14 PM
You could also use regexp (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/regexp.html#1193136):

<script type="text/javascript">
var str1 = 'teachers';
var str2 = 'teach';

re = new RegExp(str2);

if (str1.match(re)){
alert ("str2 is in str1");
} else {
alert ("str2 is not in str1");
}
</script>