返回指定位置的字元
var str="hello world!";
document.write("the first character is: " + str.charat(0) + "
");document.write("the second character is: " + str.charat(1) + "
");document.write("the third character is: " + str.charat(2));
//返回
the first character is: h
the second character is: e
the third character is: l
複製**
返回指定位置的字元的 unicode 編碼,這個返回值是 0 - 65535 之間的整數
var str="123"
document.write( str.charcodeat(0) + "
");document.write( str.charcodeat(1) + "
");document.write( str.charcodeat(2));
//返回值
4950
51複製**
用於連線兩個或多個字串
var str1="hello ";
var str2="world!";
document.write(str1.concat(str2));
//hello world!
複製**
其實直接用+連線兩個字串即可
可返回某個指定的字串值在字串中首次出現的位置,對大小寫敏感,如果要檢索的字串值沒有出現,則返回 -1
var str="hello world!"
document.write(str.indexof("h") + "
")document.write(str.indexof("w") + "
")document.write(str.indexof("w"))
//返回
0-1 //對大小寫敏感
6複製**
提取字串的某個部分,並以新的字串返回被提取的部分
slice(start,end)
,start為起始下標(包括 start),end為結尾下標(不包括 end),若未指定end,則要提取的子串包括 start 到原字串結尾的字串,start和end都可以為負數,表示從後面開始算起,-1為最後的字元
5.1document.write(str.slice(6));
//返回
複製**
未指定end
5.2
var str="hello world!";
document.write(str.slice(0,2));
//不包括下標2
he複製**
將字串進行拆分並返回乙個新的陣列
6.1
var str="how are you doing today?"
document.write(str.split(" ") + "
"); //空格
document.write(str.split("") + "
") //空
document.write(str.split(" ",3)) //空格,返回的陣列長度
//返回
how,are,you,doing,today? //空格斷開形成乙個字串
h,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,? //引數為空則每個字元為乙個元素
how,are,you //只返回三個陣列元素
複製**
在字串中抽取從 start 下標開始的指定數目的字元
obj.substr(start,length);
start為必選,起始下標,可選負數,length為可選,選中的字元個數,若不填,則返回從start開始到結束的字串
7.1
var str="hello world!";
document.write(str.substr(3));
//返回值
lo world!
複製**
從下標為3開始選,到字串結束
7.2
var str="hello world!";
document.write(str.substr(3,2));
//返回值
lo複製**
從下標為3開始選,選兩個字元
提取字串中介於兩個指定下標之間的字元
obj.substring(start,end);
start為必選,起始下標(包括),必須為非負數,stop為可選,結束下標(不包括),必修為非負數。省略stop引數,那麼會選取到字串結尾
8.1
var str="hello world!";
document.write(str.substring(3));
//lo world!
複製**
從下標為3開始選取,直到末尾
8.2
var str="hello world!";
document.write(str.substring(3,5));
//lo
複製**
從下標為3開始選取,直到下標為5,(不包括5)
把字串轉換為小寫
var str="hello world!";
document.write(str.tolowercase());
//hello world!
複製**
把字串轉換為大寫
轉為字串`obj.tostring()
在字串中用一些字元替換另一些字元,對大小寫敏感
13.1
var str="visit microsoft!";
document.write(str.replace(/microsoft/,"w3school"));
//visit w3school!
複製**
用w3school代替microsoft,但在字串只進行一次
13.2
var str="welcome to microsoft! welcome to microsoft!";
document.write(str.replace(/microsoft/g, "w3school"));
//welcome to w3school! welcome to w3school!
複製**
加了關鍵字g,為全域性搜尋,字串所有的microsoft都被替換為w3school
用於檢索字串中指定的子字串,返回匹配的第乙個位置,對大小寫敏感,無匹配字元則返回-1
var str="hello world!";
document.write(str.search("world") + "
");document.write(str.search("world") + "
");//6-1
複製**
去掉字串開頭和結尾空格
var mystr=" hello world ";
var trimstr=mystr.trim(); //hello world
複製**
JS字串操作
字串中常用的方法 let str werwafdgaewfgrjyyu str.length 字串長度 str 0 第乙個字元 str str.length 1 最後乙個字元 str 100000 undefined 不存在這個索引 for let i 0 i charat charcodeat s...
js 操作字串,
字串補全 12345 padstart 7,0 0012345 字串不足7位,在頭部補充不足長度的目標字串 12345 padend 7,0 1234500 在尾部進行字串補全 字串轉換成陣列 array.of 3,4,5 3,4,5 字串重複輸出 var str 1,2,3 重複輸出5遍 cons...
js操作字串
1 字串合併操作 var inum01 12 var inum02 24 var snum03 12 var str abc alert inum01 inum02 彈出36 alert inum01 snum03 彈出1212 數字和字串相加等同於字串相加 alert snum03 str 彈出1...