The last occurrence of a character or string in a JavaScript lookup string

Source: Internet
Author: User

In JS, to implement the last occurrence of a character or string in a lookup string we can use the LastIndexOf () method

String.LastIndexOf (string, num)
String.LastIndexOf (String)

The LastIndexOf () method returns the last occurrence of a specified string value, which is searched forward at the specified position in a string

<script language= "JavaScript" >
<!--
var myString = new String ("Hello World, here I am!");
document.write (Mystring.lastindexof ("e") + ' <br> ');

document.write (Mystring.lastindexof ("L", 3));

Document.close ();

-->

</script>


Tips and comments
Note: the LastIndexOf () method is case sensitive!

Note: If the string value to retrieve does not appear, the method returns-1.


Instance
In this case, we'll do a different search within the "Hello world!" string:

<script type= "text/web Effects"

var str= "Hello world!"
document.write (Str.lastindexof ("Hello") + "<br/>")
document.write (Str.lastindexof ("World") + <br/ > ")
document.write (Str.lastindexof (" World "))

</script> output of above code:

0
-1
6

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.