Java Script 字串操作

2021-08-27 22:16:53 字數 2343 閱讀 5677

js中常用幾種字串操作:

1.字串顯示操作方法:

var str='hello world!';

str.big();

//用大號字型顯示字串;

str.small();

//使用小字號來顯示字串;

str.bold();

//使用粗體顯示字串;

str.strike();

//使用刪除線來顯示字串;

str.italics()

//使用斜體顯示字串;

str.fontsize(size);

//使用指定的尺寸來顯示字串;

str.fontcolor(color);

//使用指定的顏色來顯示字串;

str.link(nrl);

//將字串顯示為鏈結;

2.charat();charcodeat()和fromcharcode()用法:

var str='hello world!';

str.charat(1);

//返回指定位置的字元;

//返回字元為:e ;

str.charcodeat(1);

//返回指定位置的字元的 unicode 編碼;

//返回字元編碼為:101 ;

string.fromcharcode(72,69,76,76,79);

//從字元編碼建立乙個字串;

//所建立字串為:hello ;

3.concat()用法:

var str1="hello ";

var str2="world!";

str1.concat(str2);

//用於連線兩個或多個字串,返回新的字串;

//新的字串為:hello world! ;

4.indexof()和lastindexof()用法:

var str="hello world!";

str.indexof("hello");

//返回:0 ;

str.indexof("world");

//返回:-1 ;

str.indexof("world");

//返回:6 ;

//該方法可返回某個指定的字串值在字串中首次出現的位置;

str.lastindexof("hello");

//返回:0 ;

str.lastindexof("world");

//返回:-1 ;

str.lastindexof("world");

//返回:6 ;

//該方法可返回乙個指定的字串值最後出現的位置,在乙個字串中的指定位置從後向前搜尋;

//indexof() 方法對大小寫敏感!

5.split()和substr();substring()用法:

var str="how are you doing today?";

str.split(" ");

//返回值為:['how','are','you','doing','today'] ;

str.split("");

//返回值為:['h','o','w',' ','a','r','e',' ','y','o','u',' ','d','o','i','n','g',' ','t','o','d','a','y','?'] ;

str.split(" ",3); //以空格分割字串,返回陣列最大長度為3;

//返回值為:['how','are','you'] ;

//該方法用於把乙個字串分割成字串陣列,返回陣列;

var str="hello world!";

str.substr(3,7); //3為開始位置,7為長度;

//返回值為:'lo worl' ;

//該方法可在字串中抽取從 start 下標開始的指定數目的字元;

var str="hello world!";

str.substring(3,7); //3為開始位置,7為停止位置;

//返回值為:'lo w' ;

//該方法用於提取字串中介於兩個指定下標之間的字元;

6.tolowercase()和touppercase()用法:

var str="hello world!";

str.tolowercase();

//返回值為:'hello world!' ;

//該方法用於把字串轉換為小寫,返回乙個新的字串;

str.touppercase();

//返回值為:'hello world!' ;

//該方法用於把字串轉換為大寫,返回乙個新的字串;

JavaScript 字串操作

1.函式 split 功能 使用乙個指定的分隔符把乙個字串分割儲存到陣列 例子 str jpg bmp gif ico png arr str.split arr是乙個包含字元值 jpg bmp gif ico 和 png 的陣列 2.函式 join 功能 使用您選擇的分隔符將乙個陣列合併為乙個字串...

javascript 字串操作

字串拼接,請將字串 hello 與字串 python 進行拼接,用變數m接收 m hello python js的字串拼接效果同python,直接使用加法運算子就可 字串轉整數,請將字串 1 轉換為整數1,用變數res接收 res parseint 1 說明 返回值 parseint 字串 如果字串...

JavaScript 字串的簡單操作

1 substring substring 可以接收兩個引數 不能為負值 開始位置和結束位置,擷取後返回新的字串,其內容是從start處到end 1處的所有字元。若結束引數 end 省略,則表示從start位置一直擷取到最後 slice 方法與substring 方法基本類似,兩個引數也分別對應著開...