茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    JavaScript的String字符串對象常用操作總結(jié)
    來源:易賢網(wǎng) 閱讀:1214 次 日期:2016-06-20 17:07:23
    溫馨提示:易賢網(wǎng)小編為您整理了“JavaScript的String字符串對象常用操作總結(jié)”,方便廣大網(wǎng)友查閱!

    String對象用于存儲字符串數(shù)據(jù),這里我們做了JavaScript的String字符串對象常用操作總結(jié),需要的朋友可以參考下

    創(chuàng)建String對象方式

    聲明:String對象的方法也可以在所有基本字符串值中訪問到。

    調(diào)用構(gòu)造函數(shù)String():

    var str = new String();

    var str = new String('hello world');//初始化str,str.length = 11;

    String訪問及查找的方式

    1.訪問(通過索引)

    (1)charAt()或[]

    1個參數(shù),參數(shù)為字符位置,返回字符

    var strValue = new String('hello world');

    console.log(strValue.charAt(1));//e

    console.log(strValue[1]);//e,IE7及以下版本使用這種方式,會返回undefined

    (2)charCodeAt()

    1個參數(shù),參數(shù)為字符位置,返回字符編碼

    var strValue = new String('hello world');

    console.log(strValue.charCodeAt(1));//101

    2.查找位置

    (1)indexOf()

    第一個參數(shù)為指定子字符串,第二個參數(shù)為檢索位置。返回索引,如果沒有找到則返回-1

    var str = 'hello world'

    str.indexOf('l');//2,返回找到的第一個字符的位置

    str.indexOf('l',6);//9

    (2)lastIndexOf()

    與indexOf()的區(qū)別在于,lastIndexOf()是從字符串的末尾向前搜索子字符串

    字符方法

    1.擴展字符串

    concat()

    接受任意數(shù)量參數(shù),用于將一個或多個字符串拼接起來,返回拼接得到新的字符串副本。

    var str = new String('hello');

    var result = str.concat(' world');

    console.log(result);//hello world

    typeof result//"string"

    2.獲取子字符串方法

    slice(),substr(),substring(),這三個方法都會返回被操作字符串的子字符串副本,而且也都接受1或2個參數(shù),前閉后開[)

    (1)slice()

    var str = 'hello';

    str.slice(0,2);//"he",第一個參數(shù)指定字符串開始的位置,第二個參數(shù)表示字符串到哪里結(jié)束

    str.slice(-3);//"llo",o代表-1,依次倒數(shù),-3代表倒數(shù)第三個的l

    str.slice(-2,-1);//"l",同理,-2代表倒數(shù)第二個l,-1代表倒數(shù)第一的o

    (2)substring()

    var str = 'hello';

    str.substring(0,2);//"he",此時的參數(shù)意義同str.slice(0,2)

    str.substring(-3);//"hello",substring()方法會把所有負值參數(shù)轉(zhuǎn)換為0

    str.substring(-3,-2);//"",同上

    (3)substr()

    var str = 'hello';

    str.substr(1,2);//"el",第一個參數(shù)指定字符串的開始位置,第二個參數(shù)指定的則是返回的字符個數(shù)

    str.substr(-3);//"llo",此時的參數(shù)意義同str.slice(-3)

    str.substr(-3,-1);//"",substr()方法會將負的第二個參數(shù)轉(zhuǎn)換為0

    substr()方法傳遞負值時在IE中存在問題,它會返回原始的字符串,IE9修復了這個問題

    3.將字符串轉(zhuǎn)換為數(shù)組

    split()

    基于指定的分隔符(可以是字符串,也可以是RegExp對象)將字符串分割成多個子字符串,并將結(jié)果放在一個數(shù)組中,可接受可選的第二個參數(shù),用于指定數(shù)組的大小,返回數(shù)組。

    var color = 'blue,red,orange';

    color.split();//["red,blue,orange"],長度為1

    color.split(',');//["blue", "red", "orange"],長度為3

    var color = 'blue-red-orange';

    color.split('-');//["blue", "red", "orange"],長度為3

    color.split(',',2);//["blue", "red"]

    4.字符串大小寫轉(zhuǎn)換

    toLowerCase(),toUpperCase()

    var str = 'hello';

    str.toUpperCase();//"HELLO"

    str.toLowerCase();//"hello"

    5.刪除字符串空格方法

    trim()

    刪除字符串中前置以及后綴的所有空格,然后返回結(jié)果副本。

    var str = ' hello world ';

    str.trim()//"hello world"

    6.字符串的模式匹配方法

    (1)match()

    參數(shù):只接受一個參數(shù),要么是一個正則表達式,要么是一個RegExp()對象。

    返回:數(shù)組。數(shù)組中的第一項是與整個模式匹配的字符串,之后的每一項(如果有)保存著正則表達式捕獲組匹配的字符串

    本質(zhì)上與調(diào)用exec()相同。

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

    var pattern = /.at/;

    var matches = text.match(pattern);

    matches // ["cat"]

    matches.input // "cat, bat, sat, fat"

    matches.index // 0

    (2)search()

    參數(shù):與match()方法相同。

    返回:字符串中第一個匹配項的索引,如果沒有匹配項,則返回-1。

    search()方法始終從前向后找

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

    var pattern = /at/;

    text.search(pattern) // 1

    (3)replace()

    參數(shù):接收兩個參數(shù),第一個參數(shù)可以是一個RegExp對象或者一個字符串(這個字符串不會轉(zhuǎn)換成正則表達式),第二個參數(shù)可以是一個字符串或者一個函數(shù)。

    如果 第一個參數(shù)是字符串,那么只會替換第一個子字符串。要想替換所有子字符串,唯一的辦法就是提供一個正則表達式,而且要指定全局標志(g)標志。

    如果 第二個參數(shù)是字符串,那么還可以使用一些特殊的字符序列,將正則表達式操作得到的值插入到結(jié)果字符串中。

    也可以是函數(shù),傳遞給函數(shù)的參數(shù)依次是模式的匹配項,模式的匹配項在字符串中的位置,和原始字符串。在正則表達式定義了多個捕獲組的情況下,傳遞給函數(shù)的參數(shù)依次是模式的匹配項,第一個捕獲組的匹配項,以此類推,但最后兩個參數(shù)分別是模式的匹配項在字符串中的位置和原始字符串。

    名單

    var text = 'xxx-love-xxx';

    var pattern = /xxx/g;

    var result = text.replace(pattern,'2')

    result// "2-love-2"

    text.replace(/(xxx)-\w{4}-(xxx)/g,'I love YOU');//"I love YOU"

    var text = 'xxx-love-xxx';

    var pattern1 = /xxx/g;

    var result = text.replace(pattern1,'$$')

    result// "$-love-$"

    var result = text.replace(pattern1,'$&2')

    result//"xxx2-love-xxx2"

    var result = text.replace(pattern1,'$\'')

    result//"-love-xxx-love-"

    更多信息請查看網(wǎng)絡編程
    易賢網(wǎng)手機網(wǎng)站地址:JavaScript的String字符串對象常用操作總結(jié)

    2026上岸·考公考編培訓報班

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)