1. 程式人生 > >js 操作字串的方法

js 操作字串的方法

字串分割的一些方法:

split ( separator, howmany ) 方法

  • 第一個引數 separator  字串或正則表示式,從引數指定的地方分割字串

  • 第二個引數 howmany  指定返回的陣列的最大長度

如果設定了該引數,返回的陣列中元素的個數不會多於這個引數指定的最大長度

如果沒有設定該引數,則整個字串都會被分割,不會考慮長度

例:

var str="2012|12|21|您好|我好"

var arr=str.split( "|",4 )

console.log( arr )        // ["2012", "12", "21", "您好"]

var arr=str.split( "|" )

console.log( arr )       // ["2012", "12", "21", "您好", "我好"]

trim ( str ) 方法

  • 無引數,有返回值 ( 字串型別 ),去掉字串兩端的空格

var str = "    string     "

str = str.trim ( )

console.log ( "==="+str+"===" )   // ===string===

 

concat ( str1, str2... ) 方法

  • 有多個引數都是字串型別的,返回值是字串型別的,拼接字串

var str1 = " my "

var str2 = " name "

var str3 = " is "

var str4 = " sina "

console.log ( str1+str2+str3+str4 )   // my name is sina



str4 = str1.concat ( str2,str3,str4 )

console.log(str4)   // my name is sina

console.log("hello:".concat(" world"))    // hello: world

charAt ( ) 方法

  • 引數是一個數字型別(索引).返回值就是指定位置的字串

var str="abcdefg";

str=str.charAt(3);  //根據索引返回的是指定索引位置的字串

console.log(str);   //d

 

charCodeAt ( ) 方法

  • 引數是一個索引,返回值是這個指定位置字串的ASCII碼值

var str="abcdef";

var num=str.charCodeAt(3);

console.log(num);  //100

 

slice ( ) 方法

  • 第一個引數是開始的索引 , 第二個引數是結束的索引

  • 從開始的索引位置開始擷取 , 到結束索引的前一個結束

  • 返回值是擷取後的字串,可以只有一個引數,一直從該位置到最後

var str="onclick";

str=str.slice(1,5);

console.log(str);//ncli

var str="onclick";

str=str.slice(1);

console.log(str);//nclick

substring ( ) 方法

  • 第一個引數是開始的索引,第二個引數是結束的索引

  • 從開始的索引位置開始擷取,到結束索引的前一個結束

  • 返回值是擷取後的字串,可以只有一個引數,一直從該位置到最後  用法同slice()

var str="hello world";

str=str.substring(3,9); // lo wor

console.log(str);

var str="hello world";

str=str.substring(3);

console.log(str); // lo world

substr ( )

  • 第一個引數是開始的索引,第二個引數是要擷取字串的長度,返回值是字串

var str="hello world"

console.log( str.substr(3,7) )   // lo worl

console.log( str.substr(3) )     // lo world

indexOf ( "要查詢的字串",從某個索引位置開始 ) 

  • 返回的是該字串的索引,找不到則返回 -1

var str="abcdefg";

if(str.indexOf("bc")!=-1){

    str=str.replace("bc","xy");

    console.log(str);

}else{

    console.log(str);

}

//axydefg

lastIndexOf ( "要找的字串",位置 )

  • 從後向前找字串,找不到則是-1,索引仍然是從前向後開始

  • 如果省略第二個引數,將從字串的最後向前找

  • 不省略第二個引數,則從第二個引數值的位置向前找。

var str="what where when";

console.log(str.lastIndexOf("wh")); // 11

console.log(str.lastIndexOf("wh",6)); // 5

replace ( "找替換的字串","新的字串" ) 

  • 返回值是替換後的字串

var str="what what what";

var newStr=str.replace("ha","wt");

console.log(newStr);//wwtt what what

//全域性標誌替換所有匹配的字串

var str="what what what";

var newStr=str.replace(/ha/g,"wt");

console.log(newStr);//wwtt wwtt wwtt

search ( "正則表示式" )

  • 找字串,找到則返回索引,找不到則返回-1

var str="hello world";

console.log(str.search("wo"));//6

console.log(str.search("ow"));//-1

 

toLocaleUpperCase ( )  轉大寫

toLocaleLowerCase ( )  轉小寫

var str="What your Name";

console.log(str.toLocaleUpperCase());//WHAT YOUR NAME

console.log(str.toLocaleLowerCase());//what your name

localeCompare ( 字串 )

  • 返回的是  >0前面大   <0後面大  0相同

var str1="my";

var str2="our";

console.log(str1.localeCompare(str2)); //-1