javascript基礎之字串方法

來源:互聯網
上載者:User

標籤:字串   indexof   trim   轉換   編碼   replace   star   ...   bcd   


1:屬性length
就是擷取字串的長度
注意:中文、數字、英語字母、空格,都是1個長度
eg:
"快樂大本營 oh".length
//8
var str = ‘abc‘.length;
//3
2: 屬性prototype
給對象(String)添加屬性或方法,並且添加的方法或屬性在所有的執行個體上都是通用的
eg:
String.prototype.trim = function(){
return this.replace(/(^\s*)|(\s*$)/g, ""); 
}
3:concat
串連兩個或多個字串
原字串沒有更改,返回一個新字串
stringObject.concat(stringX,stringX,...,stringX);
var str1="Hello "
var str2="world!"
var newstr=str1.concat(str2);
console.log(str1);
console.log(str2);
console.log(newstr);
//Hello
//world!
//Hello world!
4:charAt
返回指定位置的字元,從0開始。如果index是個不存在的數字,返回一個Null 字元串。
stringObject.charAt(index)
eg:
"abcdef".charAt(1);
//"b"
5:charCodeAt
返回指定位置的字元的 Unicode 編碼,如果index是個不存在的數字,返回一個NaN。
stringObject.charCodeAt(index)
eg:
var str="Hello";
console.log(str.charCodeAt(2))
//108
5:slice
方法可截取字串的某個部分,並以新的字串返回被截取的部分。
stringObject.slice(start,end)
從start下標開始,不包括end
end沒有就是提取到最後。
eg:
var str="Hello happy world!"
var newstr=str.slice(6);
console.log(newstr);
console.log(str);
//happy world!
//Hello happy world!
6:subString
提取字串中,介於兩個指定下標之間的字元
從start下標開始,不包括end
注意:start 與 stop 相等,返回的一個空串。
start 比 stop 大,先交換這兩個參數,再去提取字串。
stringObject.substring(start,stop)
var str="Hello world!";
var newstr=str.substring(3);
console.log(newstr);
console.log(str);
//lo world!
//Hello world!
eg:
var str="Hello world!"
var newstr=str.substring(7,3);
console.log(newstr);
console.log(str);
//lo w
//Hello world!
7:subStr
在字串中抽取從 start 下標開始的指定數目的字元。
stringObject.substr(start,length)
eg:
var str="Hello world!"
var newstr=str.substr(3);
console.log(newstr);
console.log(str);
//lo world!
//Hello world!
8:indexOf
某個指定的字串值searchvalue在字串中首次出現的位置。
stringObject.indexOf(searchvalue,fromindex)
fromindex指的是字串中開始檢索的位置
檢索的字串值沒有出現,返回 -1。
var str="Hello world!"
var newstr=str.indexOf(‘ll‘);
console.log(newstr);
//2
9:lastIndexOf
某個指定的字串值searchvalue在字串中最後出現的位置。
stringObject.lastIndexOf(searchvalue,fromindex)
var str="Hello world!"
var newstr=str.lastIndexOf(‘w‘);
console.log(newstr);
//6
10:search
擷取字元在字串中的位置
stringObject.search(regexp)
未找到任何匹配的子串,則返回 -1。
不執行全域匹配
var str="Hello world!"
var newstr=str.search(/world/);
console.log(newstr);
//6
把一個字串分割成字串數組。
注意‘‘和‘ ‘;
var str="Hello world"
var strarr=str.split(" ");
console.log(strarr);
//["Hello", "world"]
var str="Hello,world"
var strarr=str.split(",");
console.log(strarr);
//["Hello", "world"]
var strarr="hello".split("");
console.log(strarr);
//["h", "e", "l", "l", "o"]
11:replace
字串替換
eg:
"abcd".replace("a","0");
//"0bcd"
eg:
var str="Hello world!"
console.log(str.replace(/world/, "W"))
//Hello W!
12:toLowerCase
把字串轉換為小寫
stringObject.toLowerCase()
原字串不變,返回的是個字串
var str="Hello World!"
var newstr=str.toLowerCase();
console.log(str);
console.log(newstr);
//Hello World!
//hello world!
13:toUpperCase()
把字串轉換為大寫
stringObject.toUpperCase()
原字串不變,返回的是個字串
var str="Hello World!"
var newstr=str.toUpperCase();
console.log(str);
console.log(newstr);
//Hello World!
//HELLO WORLD!

javascript基礎之字串方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.