indexOf() and lastIndexOf() methods in js

indexOf() and lastIndexOf() methods - Find a string within another string The

    indexOf() and lastIndexOf() methods are used to find if a string contains another string. A string contained within another string is often called a string. These two methods can be used when only part of a string of information is required. For example, in a "quiz" program, the user enters a text answer and needs to check whether the string contains certain keywords.
    Both the indexOf() and lastIndexOf() methods take two parameters:
    1 The string to be searched
    2 The character position to start searching (optional) The
    character position starts from 0. If the second parameter is not included, the indexOf() and lastIndexOf() methods are searched from the beginning of the string
    . The return value of the indexOf() and lastIndexOf() methods is the position of the found substring in the string. It is also 0-based, so if the character If the substring is found at the beginning of the string, it returns 0. If the substring is not found, it returns -1.
    For example, to find the substring "Jeremy" in the string "Hello jeremy. How are you Jeremy", you can use the following code:
    var myString = "Hello jeremy.How are you Jeremy";
    var foundAtPosition;

    foundAtPosition = myString.indexOf("Jeremy");
    alert(foundAtPosition);

    The above code will pop up a message box containing the array 26, which is the character position of "Jeremy", why is it 26? Apparently it's the second occurrence of "Jeremy" in the string, not position 6 where the first "jeremy" occurs. This is because JavaScript is case-sensitive, and if the input is IndexOf(), not indexOf(), JavaScript will report an error. Also, "jeremy" is not the same as "Jeremy", and capitalization errors are so common that even programming experts can easily make such mistakes. So it is best to pay special attention when programming.
    The indexOf() method was introduced earlier, but the difference with the lastIndexOf() method is that the indexOf() method searches backwards from the beginning of the string or the position specified by the second parameter, while the lastIndexOf() method starts from the end of the string or Searches towards the beginning of the string at the specified position.
    
    var myString = "Hello Jeremy.How are you Jeremy";
    var foundAtPosition;

    foundAtPosition = myString.indexOf("Jeremy");
    alert(foundAtPosition);

    coundAtPosition = myString.lastIndexOf("Jeremy");
    alert(foundAtPosition);

    In this example, using indexOf() for the first time, find the first "Jerermy" (change "jeremy" in the previous example to the correct case), the alert box shows this result, which is string position 6, Then use lastIndexOf() to find, it starts from the end of the string, so the first "Jeremy" found is the last "Jeremy" at character position 26 in the string, so the second alert box shows the result is 26.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326641738&siteId=291194637