1. 程式人生 > >javascript 字串方法

javascript 字串方法


//1.  str.charAt(index) //一個介於0 和字串長度減1之間的整數
var str1 ='abcdef';
var a = str1.charAt(1);//b  0 1 1代表第二個字元
console.log(a);
//concat(str)  方法將一個或多個字串與原字串連接合並,形成一個新的字串並返回。 需要注意陣列也有該方法
var a2 = 'aaaa'.concat('bbb');//"aaaabbb"(感覺不如直接用+...)
 a =['c','lalal','hhe'].concat(['1','xxx']);//["c", "lalal", "hhe", "1", "xxx"] 兩個陣列相加
//endsWith(str)方法用來判斷當前字串是否是以另外一個給定的子字串“結尾”的,根據判斷結果,都不要用它,ie沒實現 a= 'xxx.jpg'.endsWith('jpg');//true 感覺比indexof要簡潔, 但是這個屬於 ECMAScript 2015(ES6)規範 a = '馬薩卡'.endsWith('卡');//true //includes(str) 方法用於判斷一個字串是否包含在另一個字串中,根據情況返回true或false。 a = 'lalal'.includes('laa');//false 這個方法感覺沒啥用,可以用indexof替換 //indexOf() 方法返回呼叫 String 物件中第一次出現的指定值的索引,開始在 fromIndex進行搜尋。如果未找到該值,則返回-1。
//找到了就返回該第一次出現的位置,沒找到就返回-1 //str.indexOf(searchValue[, fromIndex]) 指定數字,從該數字往右邊查詢 0開始算,找到一個為準 a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('JavaScript');//45 a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('https');//0
a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('tps');//2 a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('tpss');//-1 //str.lastIndexOf(searchValue[, fromIndex]) //指定數字,從該數字往左邊查詢,0開始算 5是第六個字元,找到一個人為準 a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.indexOf('/');//6 a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.lastIndexOf('/');//80 a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.lastIndexOf('/',5);//-1 a ='https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array'.lastIndexOf(':',5);//5 // str.match(regexp); 當一個字串與一個正則表示式匹配時, match()方法檢索匹配項。 //str.replace(regexp|substr, newSubStr|function) replace() 方法返回一個由替換值替換一些或所有匹配的模式後的新字串。 //模式可以是一個字串或者一個正則表示式, 替換值可以是一個字串或者一個每次匹配都要呼叫的函式。 //注意:原字串不會改變。 var x1 = 'xxxaxxx' a = x1.replace('a','b');//xxxbxxx console.log(x1);//xxxaxxx //str.search(regexp) search() 方法執行正則表示式和 String物件之間的一個搜尋匹配。 匹配正則,感覺正則需要聯絡,不應強記 //str.slice(beginSlice[, endSlice]) 提取字串,指定開始和結束; //特點:包含開始不包含結束,從0開始算 第二個引數為負數,表示倒數第幾個,-1表示倒數第一個(注意這裡不是從0開始算了),第二個引數不寫,表示擷取完 a = '0123456789'.slice(0,2); //01 索引 0 1 的值 a = '0123456789'.slice(2,2); // 空 沒擷取到 a = '0123456789'.slice(2,-1); // 2345678 索引2到 倒數第一個 a = '0123456789'.slice(4,); // 456789 擷取完 a = '0123456789'.slice(-2); // 89 倒數第二到完, a = '0123456789'.slice(-4,-3); // 6 倒數第4到倒數第3 //str.split([separator[, limit]]) 分割為陣列.limit表示最大的陣列長度 //就是把指定字串之間的內容都提取出來,表示陣列格式,所以 a = '/https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/split/'.split('/') //["", "https:", "", "developer.mozilla.org", "zh-CN", "docs", "Web", "JavaScript", "Reference", "Global_Objects", "String", "split", ""] //str.substr(start[, length]) 方法返回一個字串中從指定位置開始到指定字元數的字元。 //從指定位置,擷取指定個數的字串, 第二個引數不寫,表示擷取完,負數表示倒數第幾個開始擷取 a = 'abcdefghtjk'.substr(2,5); // cdefg a = 'abcdefghtjk'.substr(-1); // k a = 'abcdefghtjk'.substr(1); // bcdefghtjk //str.substring(indexStart[, indexEnd]) 指定開始和結束的字串,不包含結尾 //slice() 比 substring() 要靈活一些,因為它允許使用負數作為引數。因此可以儘量用slice() a = '0123456789'.substring(1,2);//1 a = '0123456789'.substring(-2);//0123456789 不支援負數以及其他值() //轉大小寫 無需多言 //String.prototype.toLowerCase() //String.prototype.toUpperCase() //str.trim() 不影響原來的字串,返回新的字串 a = ' a v c '.trim();//a v c 不會把裡面的空格也去掉 //str.valueOf() Object也有valueOf 方法, valueOf主要把物件轉換成一個基本資料的值 //String 物件的 valueOf 方法返回一個String物件的原始值。該值等同於String.prototype.toString()。 //JavaScript 呼叫 Object.valueOf() 方法用來把物件轉換成原始型別的值(數值、字串和布林值)。 //你很少需要自己呼叫此函式;當遇到一種需要轉換成一個原始值情況時候, JavaScript 會自動呼叫此函式。 // var strFullPath = window.document.location.href;// http://localhost:8080/oa/index.do // var strPath = window.document.location.pathname;// /oa/index.do // var pos = strFullPath.indexOf(strPath); // var prePath = strFullPath.substring(0, pos); // http://localhost:8080 // var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1); // /oa // var url = prePath + postPath; // http://localhost:8080/oa var fullPath = location.href; var pathname = location.pathname; a= '/oa/xx/index.do'.slice(1).indexOf('/') // 2 去掉前面第一個'/' 得到第二個'/'的位置 a+1; a = '/oa/xx/index.do'.slice(0,a+1); //oa // 得到/oa //location的值獲取 一定要精通字串的使用 // location.href // "https://developer.mozilla.org/zh-CN/docs/Web/API/Location" // location.host // "developer.mozilla.org" // location.pathname // "/zh-CN/docs/Web/API/Location" console.log(a);