In QT, indexof () and lastindexof () are used to find the string location.
Source: Internet
Author: User
Homepage? Javascript? Indexof () and lastindexof () search string location published on by admin two methods to find the sub-string from the string: indexof () and lastindexof (). Both methods search for a given string from a string, and then return the position of the substring (if there is no position of the substring,-1 is returned ). The difference between the two methods is that the indexof () method searches for strings from the beginning of the string, while the lastindexof () method searches for substrings forward from the end of the string. VaR stringvalue = "Hello World"; alert (stringvalue. indexof ("O"); // 4 alert (stringvalue. lastindexof ("O"); // the first occurrence position of the 7 sub-string "O" is 4, that is, "O" in "hello"; the last occurrence position is 7, that is, "O" in "World ". If "O" appears only once in this string, then indexof () and lastindexof () will return the same location value. Both methods can accept the optional second parameter, indicating the position in the string to start searching. In other words, indexof () Searches backward from the specified position, ignoring all characters before the specified position; while lastindexof () Searches forward from the specified position, ignore all characters after this position. Take the following example: var stringvalue = "Hello World"; alert (stringvalue. indexof ("O", 6); // 7 alert (stringvalue. lastindexof ("O", 6) // 4 after passing the second parameter 6 to the two methods, the opposite result is obtained. This time, because indexof () is a backward search starting from position 6 (The letter "W"), "O" is found in position 7, so it returns 7. Lastindexof () is a forward search from 6. The result shows "O" in "hello", so it returns 4. When the second parameter is used, you can call indexof () or lastindexof () cyclically to find all matched substrings, as shown in the following example: vaR stringvalue = "lorem ipsum dolor sit Amet, consectetur adipisicing elit"; var positions = new array (); var Pos = stringvalue. indexof ("e"); While (Pos>-1) {positions. push (POS); Pos = stringvalue. indexof ("e", POS + 1);} alert (positions); // "3, 24, 32, 35, 52" in this example, the indexof () method is added to start searching, A long string is traversed. In addition to the loop, the initial position of "e" in the string is first found. After the loop is entered, 1 is added to the last position passed to indexof () each time. This ensures that each new search starts after the last string found. The position returned by each search is saved in the array positions for future use. Related posts: String-type charat () and charcodeat () obtain the string-specific characters Concat (), slice (), substr (), substring () JavaScript push (), and pop () methods JavaScript shift () and unshift () Methods this article is published on the Javascript label as reference type. add a fixed link to favorites. read more> navigator. online Offline Detection localecompare () method HTML5 uses canvas to draw radial gradient cross-browser determine window size cookie components JavaScript insert dynamic scripts clientx and clienty determine customer zone... HTTP header information of JavaScript inert Loading Function
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.