js-在字符串中查找字符串
导读:字符串(String) 字符串(String)使用长度属性length来计算字符串的长度: 实例 var txt="Hello World!"; document.write(txt.length ; var txt="ABCDE...
字符串(String)
字符串(String)使用长度属性length来计算字符串的长度:
实例
var txt="Hello World!";
document.write(txt.length);
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.length);
在字符串中查找字符串
字符串使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置:
实例
var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");
如果没找到对应的字符函数返回-1
lastIndexOf() 方法在字符串末尾开始查找字符串出现的位置。
内容匹配
match()函数用来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。
实例
var str="Hello world!";
document.write(str.match("world") + ""); document.write(str.match("World") + "
"); document.write(str.match("world!"));
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: js-在字符串中查找字符串
本文地址: https://pptw.com/jishu/655705.html
