string类型的方法
var str1 = ' this is string1 '; var str2 = 'this is string2'; str1.indexOf('s'); //args:string return:Number str1.lastIndexOf('s'); //args:string return:Number str1.length; //return:Number str1.split(' '); //字符串分割为数组 return Array;,数组的方法join()为将数组连接成为字符串 str1.match(/\w/g); //将字符串按照匹配分割成为一个Array类型的东西, return Array; str1.charAt(3); //参数为下标(index),返回对应下标的字符串。 console.log(str1.concat(str2)); //对原来的数组没有影响。返回值为当前的这个合并之后的数组。 console.log(str1); str1.slice(0,5); //args: 1:start下标 2:end下标 return String str1.substr(5); //args: 1:start下标 2:length return String str1.trim(); //去除两端空格 str1.toUpperCase(); //转为大写 str1.toLowerCase(); //转为小写 str1.valueOf(); //返回原始值 str1.constructor; str1.prototype.tostring = function(){ alert(1) }
详细篇:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>string操作</title> </head> <body> <script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> var str1 = 'abcdefg fcdr edf'; // length 返回字符串的长度 console.log('字符串的长度为'+str1.length); //字符串的长度为18 //prototype; 扩展实例方法 $.extend(String.prototype,(function(){ function trimAll(){ return this.trim().replace(/\s+/g,''); } return { trimAll:trimAll } })()); console.log(str1.trimAll()); //abcdefgfcdredf // charAt string.charAt(index) 指定位置的字符,返回这个字符(新的string),char:字符。At:在 console.log(str1.charAt(2)); //c // charCodeAt string.charCodeAt(index) 返回指定位置的字符的Unicode编码 返回值类型number类型,因为是Unicode。 var charCodeAtT = str1.charCodeAt(2); console.log(charCodeAtT); //99 console.log(typeof charCodeAtT); //number //concat string.concat(string1, string2, ..., stringX) 连接两个或者多个字符串,返回一个新的字符串;一般用“+”代替。 var con1 = 'concat1'; var con2 = 'concat2'; console.log(str1.concat(con1,con2)); //abcd efg fcdr edfconcat1concat2 console.log(str1+con1+con2); ///abcd efg fcdr edfconcat1concat2 //fromCharCode String.fromCharCode(n1, n2, ..., nX) String的静态方法(类方法),不能通过实例调用 ,返回代表 Unicode 编码的字符。 var fromCharCode1 = String.fromCharCode(72,69,76,76,79); // 是fromCharCode不是formCharCode切记 console.log(fromCharCode1); //HELLO //indexOf string.indexOf(searchvalue,start) 指定字符首次在字符串中出现的位置 参数为字符,如果指定的字符不在字符串中,则返回-1;即没找到。 //start: 规定从字符串中的什么位置开始检索 取值范围:0~str.length-1 var indexOf1 = str1.indexOf('c'); console.log(indexOf1); //2 //lastIndexOf string.lastIndexOf(searchvalue,start) 指定字符最后一次在字符串中出现的位置 参数为字符,如果没找到,返回-1. var lastIndexOf1 = str1.lastIndexOf('c'); console.log(lastIndexOf1); //10 //string.match(regexp) 使相配,即查找匹配的字符串 返回值 Array //这个方法的行为在很大程度上有赖于 regexp 是否具有标志 g。如果 regexp 没有标志 g,那么 match() 方法就只能在 stringObject 中执行一次匹配。如果没有找到任何匹配的文本, match() 将返回 null。否则,它将返回一个数组,其中存放了与它找到的匹配文本有关的信息。 var match1 = "The rain in SPAIN stays mainly in the plain"; var n=match1.match(/ain/g); console.log(n); //["ain", "ain", "ain"] console.log($.type(n)); //array //string.replace(searchvalue,newvalue) 返回值:一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。 //searchvalue:规定子字符串或要替换的模式的 RegExp 对象。请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。 //newvalue:要替换的新字符串 var replace1 = match1.replace('ain','AIN'); console.log(replace1); //The rAIN in SPAIN stays mainly in the plain var replace2 = match1.replace(/ain/gi,'AIN'); console.log(replace2); //The rAIN in SPAIN stays mAINly in the plAIN //string.search(searchvalue) 与indexOf的用法差不多,但是search可以接受正则表达式,indexO则不能接收。还有就是search只能接受一个参数,但是indexOf可以接受两个参数。与指定查找的字符串或者正则表达式相匹配的String 对象起始位置。返回值类型:number console.log(match1); //The rain in SPAIN stays mainly in the plain var search1 = match1.search('ain'); console.log(search1); //5 var search2 = match1.search(/AIN/g); console.log(search2); //14 //slice string.slice(start,end) 截取字符串,有subString()和subStr(),其中subString的参数如果为负数,则会变成0,subStr的第二个参数是要截取的长度,不是下标。 //左闭右开区间 取左不取右 //截取之后的长度为len = end-start var slice1 = str1.slice(0,5); console.log(slice1); //abcde //split() string.split(separator,limit) /** * [splitT description] 字符串 * separator:可选。字符串或正则表达式,从该参数指定的地方分割 string Object。 * limit:可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。 * @type {String} * return Array */ var splitT = '172-7305-0288'; var arrSplit = splitT.split('-'); console.log(arrSplit); //["172", "7305", "0288"] var StrSplit = arrSplit.join('-'); console.log(StrSplit); //'172-7305-0288' //string.toLowerCase() 把字符串转换为小写。 //string.toUpperCase() 把字符串转换为大写 /** * [strlCase description]此方法不会改变原来的字符串 * @type {String} * return String */ var strlCase = 'Hello World'; var strToLCase = strlCase.toLowerCase(); var strToUCase = strlCase.toUpperCase(); console.log(strToLCase); //hello world console.log(strToUCase); //HELLO WORLD /** * [strTrim description]trim IE8及以下不支持原生trim方法 * @type {String} */ /** * 兼容IE8及以下写的兼容。扩展String原型方法。 */ $.extend(String.prototype,(function(){ function trim(){ return this.replace(/^\s\s*/,'').replace(/\s\s*$/,''); } return { trim:trim } })()); var strTrim = ' abcdef '; console.log('"'+strTrim.trim()+'"'); </script> </body> </html>
扩展String原型方法:
$.extend(String.prototype, function() { /** * @description 清除字符串开头和结尾的空格,只有ECMA5才支持trim方法,因此扩展String这一包装对象的去除空格方法 */ function trim() { return this.replace(/^\s*/, '').replace(/\s*$/, ''); } /** * 去除字符串中所有的空格 * @return {[type]} [description] */ function trimAll() { return this.trim().replace(/\s+/g, ''); } /** * @description 清除字符串首尾空格,并把字符串之间的多个空格转换为一个空格 */ function clean() { return this.trim().replace(/\s+/g, ' '); }; /** * @description 将“-”连接的字符串转换成驼峰式写法 */ function capitalize() { return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase(); } /** * @description 把目标字符串峰驼化 */ function camelize() { return this.replace(/-+(.)?/g, function(match, chr) { return chr ? chr.toUpperCase() : ''; }); } /** * @description验证是否是邮箱 */ function isEmail() { var reg1 = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4})$/; return reg1.test(this); } /** * @description 验证手机号码 */ function isMobile() { var reg = /^[1][3,4,5,8]\d{9}$/; return reg.test(this); } /** * @description 验证是否是日期 */ function isDate() { var pattern = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/; if (!pattern.test(this)) { alert("日期格式不对"); return false; } var arr = this.split("-"); if (arr[1].indexOf("0") == 0) { arr[1] = arr[1].substr(1); } if (arr[2].indexOf("0") == 0) { arr[2] = arr[2].substr(1); } if (parseInt(arr[1]) < 1 || parseInt(arr[1]) > 12) { alert("月份不对"); return false; } if (parseInt(arr[2]) < 1 || parseInt(arr[2]) > 31) { alert("日期的天数不对"); return false; } return true; } return { trim: trim, trimAll: trimAll, clean: trimAll, capitalize: capitalize, camelize: camelize, isEmail: isEmail, isMobile: isMobile, isDate: isDate } }());