Click to See Complete Forum and Search --> : sub string of a string


pelegk1
06-23-2003, 04:45 AM
is there a ready made function that tell's u if a string contains another string?
for example :
str1="abcdef5423"
str2="def5"
the i will get
that start position of str2 in str1 is 6

i know it available in vbscript

thanks inadvance
Peleg

Charles
06-23-2003, 05:20 AM
<script type="text/javascript">
<!--
String.prototype.position = function (s) {this.match(new RegExp('^(.*)' + s)); return RegExp.$1.length}

alert ('abcdef5423'.position('def5'))
// -->
</script>

jeffmott
06-23-2003, 07:22 AM
String.prototype.position = function (s) {...This may require a small change. As it is right now there is no way to tell the whether the search string occurs at the beginning or doesn't occur at all.

In any case, this is all really unecessary. The indexOf method will perform this job.alert('abcdef5423'.indexOf('def5'));