javascript string 方法總結

2021-09-13 12:21:06 字數 3716 閱讀 2284

1、charat()

接收乙個引數,基於0的字元位置。以單字串的形式返回給定位置的那個字元。

var strin**alue = "hello world";

console.log(strin**alue.charat(1)); //"e"

2、charcodeat()

接收乙個引數,基於0的字元位置。 返回的是字元編碼。

var strin**alue = "hello world";

console.log(strin**alue.charcodeat(1)); //101

1、concat()

用於將乙個或多個字串拼接起來,返回拼接得到的新字串,不會修改字串本身的值,只是返回乙個基本型別的字串值。

var strin**alue = "hello ";

var result = strin**alue.concat("world");

console.log(result); // "hello world"

console.log(strin**alue); // "hello"

2、slice()

擷取字串,只是返回乙個基本型別的字串值,對原始字串沒有任何影響。

如果傳兩個引數,第乙個引數是開始擷取的位置,第二個引數是結束擷取的位置。

var strin**alue = "hello world";

console.log(strin**alue.slice(3)); //"lo world"

console.log(strin**alue.slice(3,7)); //"lo w"

3、substring()

擷取字串,只是返回乙個基本型別的字串值,對原始字串沒有任何影響。

如果傳兩個引數,第乙個引數是開始擷取的位置,第二個引數是結束擷取的位置。

var strin**alue = "hello world";

console.log(strin**alue.substring(3)); //"lo world"

console.log(strin**alue.substring(3,7)); //"lo w"

4、substr()

擷取字串,只是返回乙個基本型別的字串值,對原始字串沒有任何影響。

如果傳兩個引數,第乙個引數是開始擷取的位置,第二個引數是返回的字元個數。

var strin**alue = "hello world";

console.log(strin**alue.substr(3)); //"lo world"

console.log(strin**alue.substr(3,7)); //"lo worl"

1、indexof()

接收乙個引數的時候,返回第一次出現該字元的位置

接收兩個引數的時候,第乙個是查詢的字元,第二個是開始查詢的位置。

var strin**alue = "hello world";

console.log(strin**alue.indexof("o")); //4

console.log(strin**alue.indexof("o",6)); //7

2、lastindexof()

接收乙個引數的時候,返回最後一次出現該字元的位置

接收兩個引數的時候,第乙個是查詢的字元,第二個是開始查詢的位置。

var strin**alue = "hello world";

console.log(strin**alue.lastindexof("o")); //7

console.log(strin**alue.lastindexof("o",6)); //4

這個方法會建立乙個字串的副本,刪除前置及字尾的所有空格,然後返回結果。

var strin**alue = " hello world ";

var trimmedstrin**alue = strin**alue.trim();

console.log(strin**alue); //" hello world "

console.log(trimmedstrin**alue); //"hello world"

1、tolowercase()

將字串轉換成小寫

var strin**alue = "hello world";

console.log(strin**alue.tolowercase()); //"hello world"

2、touppercase()

將字串轉換成大寫

var strin**alue = "hello world";

console.log(strin**alue.toupplercase()); //"hello world"

1、match()

只接受乙個引數,要麼是乙個正規表示式,要麼是乙個regexp物件。

var text = "cat,bat,sat,fat";

var pattern = /.at/;

var matches = text.match(pattern);

console.log(maches.index); //0

console.log(maches[0]); //"cat"

2、search()

唯一引數與match()方法引數相同,search()方法返回字串中第乙個匹配項的索引;如果沒有找到匹配項,則返回-1.

var text = "cat, bat, sat, fat";

var pos = text.search(/at/);

console.log(pos); //1

3、replace()

這個方法接收兩個引數:第乙個引數可以使乙個regexp物件或者乙個字串,第二個引數可以使乙個字串或者乙個函式。

var text = "cat, bat, sat, fat";

var result = text.replace("at","ond");

console.log(result); //"cond, bat, sat, fat"

result = text.replace(/at/g,"ond");

console.log(result); //"cond, bond, sond, fond"

這個方法比較兩個字串,並返回下列值中的乙個:

var strin**alue = "yellow";

console.log(strin**alue.localecompare("brick")); //1

console.log(strin**alue.localecompare("yellow")); //0

console.log(strin**alue.localecompare("zoo")); //-1

這個方法的任務是接收乙個或者多個字元編碼,然後將它們轉換成乙個字串。

console.log(string.fromcharcode(104,101,108,108,111)); //"hello"

Javascript String物件參考手冊

string 物件用於處理文字 字串 建立 string 物件的語法 new string s 引數 s 是要儲存在 string 物件中或轉換成原始字串的值。方法和屬性 說明constructor 返回對string物件建構函式的引 length 獲取字串的長度 charat 返回指定位置的字元 ...

Javascript String物件總結 二

引數 只接受乙個引數,要麼是乙個正規表示式,要麼是乙個regexp 物件。返回 陣列。陣列中的第一項是與整個模式匹配的字串,之後的每一項 如果有 儲存著正規表示式捕獲組匹配的字串 本質上與呼叫exec 相同。var text cat,bat,sat,fat var pattern at var ma...

JavaScript String物件的一些個方法

ff firefox,ie internet explorer 方法描述 ffie anchor 建立 html 錨。13 big 用大號字型顯示字串。13 blink 顯示閃動字串。1bold 使用粗體顯示字串。13 charat 返回在指定位置的字元。13 charcodeat 返回在指定的位置...