1. 程式人生 > >javascript基礎之字符串方法

javascript基礎之字符串方法

abcd font earch 分割 定位 lastindex toupper 參數 方法


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是個不存在的數字,返回一個空字符串。
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基礎之字符串方法