範例1:
Search a string for "welcome":
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
The result of n will be:
13
範例2:
var str = "Hello world!"
document.write(str.indexOf("Hello")
document.write(str.indexOf("World")
document.write(str.indexOf("world")
答案為:0 -1 6
當indexOf()回傳-1,表示找不到比對的值。 當indexOf()回傳0以上的數值,表示回傳比對數值的位置。
沒有留言:
張貼留言