常见方法代码

var str = "Yesterday, my life was headed in one direction. Today, it is headed in another."

//-----分割----------
document.write(str.split(" ") + "
") document.write(str.split(" ", 3)) //输出结果 //Yesterday,, my, life, was, headed,in, one, direction., Today,, it, is, headed,in, another. //Yesterday,, my, life //js 字符串中怎样检测出有多少个逗号 str.split(',').length //输出结果 3 //-----包含---------- //JS类似contains方法,用indexOf实现 if (str.indexOf("my") != -1) { //如果上面这个表达式为true,则包含,反之则不包含。 } //输出结果 true if (str.search("my") != -1) { //如果上面这个表达式为true,则包含,反之则不包含。 } //输出结果 true //正则匹配 var reg = RegExp(/my/); document.write(str.match(reg)); //有就返回匹配的字符串,没有返回null document.write(reg.test(str)); true document.write("exec:" + reg.exec(str)); //返回my //-------剪切/删除------ //删除字符串最后一个字符 str.substring(0, str.length - 1) //String.Substring str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文"; var Text = str.Substring(11);//返回 “国际通邓事文*深圳市盈基实业有限公司国际通邓事文” Text = str.Substring(11, 7);//返回 “国际通邓事文*” Text = str.Substring(str.Length - 3, 3); // 返回邓事文,即截倒数3位字符 //String.IndexOf(value, startIndex, count) //value:要查找的 Unicode 字符。 //startIndex:搜索起始位置。 //count:要检查的字符位置数。 var IndexOfValue = str.IndexOf("深圳市", 0, 10) //String.LastIndexOf Text = str.LastIndexOf("邓文").ToString();//返回-1 Text = str.LastIndexOf("邓").ToString();//返回32 Text = str.LastIndexOf("邓",8).ToString();//返回-1 Text = str.LastIndexOf("邓",20).ToString();//返回14 Text = str.LastIndexOf("邓",33).ToString();//返回32

你可能感兴趣的:(01前端,javascript)