javascript字串筆記

來源:互聯網
上載者:User

標籤:

<script type="text/javascript">var str="abcdefbc? 11 , 123 ff,6";var str2=new String("ghl");console.log("typeof:"+typeof str); // typeof Stringconsole.log("typeof:"+typeof str2); //typeof Object 封裝類//一、擷取類console.log("charAt:"+str.charAt(5)); // 返回指定位置的字元,下標從0開始 fconsole.log("charCodeAt:"+str.charCodeAt(0)) //返回指定字元的編碼 97console.log("fromCharCode:"+String.fromCharCode(97)) //編碼轉換成字元 a//二、尋找類console.log("indexOf:"+str.indexOf("cba")); //返回字串中一個子串第一處出現的索引(從左至右搜尋)。如果沒有匹配項,返回-1 ;-1console.log("indexOf:"+str.indexOf("c"));//2 返回字串中匹配的索引值console.log("lastIndexOf:"+str.lastIndexOf("c"));//返回字串中一個子串"最後一處"出現的索引(從右至左搜尋),如果沒有匹配項,返回 -1,有返回實際索引 。7console.log("search:"+str.search("bc"));//執行一個Regex匹配尋找。如果尋找成功,返回字串中匹配的索引值。否則返回 -1 。1/*indexOf和search的區別:search可以匹配Regex,search("?"),search不可以尋找?+等量詞,如需只能用正則:search(/\?/)*/console.log("search:"+str.search(/\?/));//8console.log("match:"+str.match(/\d+/g));//11,123,6  //match 檢查一個字串匹配一個Regex內容,如果麼沒有匹配返回 null。依賴於正則,不然沒有實際意義/*replace用來尋找匹配一個Regex的字串,然後使用新字串代替匹配的字串。返回一個新的字串,是用 replacement 替換了 regexp 的第一次匹配或所有匹配之後得到的。stringObject.replace(regexp/substr,replacement) */console.log("replace:"+str.replace("bc","BC"));//aBCdefbc? 11 , 123 ff,6 console.log("replace:"+str.replace(/bc/,"BC"));//aBCdefbc? 11 , 123 ff,6 console.log("replace:"+str.replace(/bc/g,"BC"));//aBCdefBC? 11 , 123 ff,6;g表示全域尋找//三、截取類/*slice() 方法可提取字串的某個部分,並以新的字串返回被提取的部分。stringObject.slice(start,end)參數    描述start    要抽取的片斷的起始下標。如果是負數,則該參數規定的是從字串的尾部開始算起的位置。        也就是說,-1 指字串的最後一個字元,-2 指倒數第二個字元,以此類推。end        緊接著要抽取的片段的結尾的下標。若未指定此參數,則要提取的子串包括         start 到原字串結尾的字串。如果該參數是負數,那麼它規定的是從字串的尾部開始算起的位置。*/console.log("slice:"+str.slice(2));//cdefbc? 11 , 123 ff,6console.log("slice:"+str.slice(-2));//,6console.log("slice:"+str.slice(1,6));//,cdef/*substring() 方法用於提取字串中介於兩個指定下標之間的字元。stringObject.substring(start,stop)與 slice() 和 substr() 方法不同的是,substring() 不接受負的參數slice() 比 substring() 要靈活一些,因為它允許使用負數作為參數。*/var str3="Hello world!"console.log("substring:"+str3.substring(3,7))//lo w  3-7之間的數/*substr() 方法可在字串中抽取從 start 下標開始的指定數目的字元。stringObject.substr(start,length)重要事項:ECMAscript 沒有對該方法進行標準化,因此反對使用它。*///toLowerCase、toUpperCase——大小寫轉換console.log("toLowerCase:"+str3.toLowerCase())//hello world! 小寫console.log("toUpperCase:"+str3.toUpperCase())//HELLO WORLD! 大寫console.log("length:"+str3.length)//返回12,從1開始計數/*concat() 方法用於串連兩個或多個字串。文法stringObject.concat(stringX,stringX,...,stringX)*/console.log("concat:"+str3.concat(str))//Hello world!abcdefbc? 11 , 123 ff,6console.log("+     :"+str3+str)//Hello world!abcdefbc? 11 , 123 ff,6var abc="hello world"var abc2=abc.split("");console.log("+     :"+abc.split("").join(" "))//h e l l o   w o r l dconsole.log("+     :"+typeof abc2)//h e l l o   w o r l dconsole.log("+     :"+abc2.push("f"))//h e l l o   w o r l dconsole.log("+     :"+abc2)//h e l l o   w o r l d/*split() 方法用於把一個字串分割成字串數組。分割成數組後可以使用數組相關的使用方法,反回一個字串數組stringObject.split(separator,howmany)一個字串數組。該數組是通過在 separator 指定的邊界處將字串 stringObject 分割成子串建立的。返回的數組中的字串不包括 separator 自身。separator    必需。字串或Regex,從該參數指定的地方分割 stringObject。howmany        可選。該參數可指定返回的數組的最大長度。如果設定了該參數,返回的子串不會多於這個參數指定的數組。            如果沒有設定該參數,整個字串都會被分割,不考慮它的長度。提示和注釋注釋:如果把Null 字元串 ("") 用作 separator,那麼 stringObject 中的每個字元之間都會被分割。注釋:String.split() 執行的操作與 Array.join 執行的操作是相反的。*/var str4="How are you doing today?"console.log("length:"+str4.length)//24console.log("split:"+str4.split(""))//H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?console.log("length:"+str4.length)//24console.log("split:"+str4.split(" "))//How,are,you,doing,today?console.log("split:"+str4.split(" ",3))//How,are,youconsole.log("split:"+str4.split("|",3))//How are you doing today? 沒有|號,返回初始值console.log("2:3:4:5".split(":"))    //將返回["2", "3", "4", "5"]console.log("2?3?4?5".split("?"))    //將返回["2", "3", "4", "5"]console.log("hello".split(""))    //["h", "e", "l", "l", "o"]</script>

 

javascript字串筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.