JS基礎-資料類型轉化和字串常用操作

來源:互聯網
上載者:User

標籤:AC   ora   取字串   子串   oracle   none   索引   ble   表達   

模組回顧

1.資料類型的轉化。數-字,字-整,字-浮。

2.字串常用操作。10種。

一、資料類型的轉化

 

通過“+”號或toString()方法將數值轉換成字串。

2.通過parseInt()將字串轉換成整型。

3.通過parseFloat()將字串轉換成浮點型。

 

<html><head><meta charset="utf-8"><title>資料類型的轉換</title><script>    var a=10;    var b=2;    //將數值型轉換為字串型    var c=a.toString();        alert(c+b);    var mes="67.3";    //將字串轉換成整型    var d=parseInt(mes);    alert(d);    //將字串轉換成浮點型    var e=parseFloat(mes);    alert(e);    var ans="oracle";    alert(parseInt(ans));//NaN Not a number    </script></head><body></body></html>
資料類型轉化

 

二、字串的常用操作

charAt()

擷取字串特定索引處的字元

toUpperCase()

將字串的所有字元轉換成大寫字母

indexOf()

返回字串中特定字串第一次出現的位置

substring()

返回字串的某個子串

slice()

返回字串的某個子串,支援負數參數,功能更強大

concat()

用於將多個字串拼加成一個字串

replace()

將字串中的某個子串以特定的字串替換

split()

將某個字串分割成多個字串,可以指定分隔字元

match()

使用Regex搜尋目標子字串

search()

使用Regex搜尋目標子字串

<!doctype html><html><head><meta charset="utf-8"><title>操作字串的常用方法</title><script type="text/javascript">    var str="I love javascript"    alert(str.length);    alert(str.length)擷取字串長度    alert(str.charAt(4));//擷取第四位字元的值    alert(str.toUpperCase());//將字串中的字母都轉換成大寫    alert(str.indexOf(‘o‘));//擷取字串中"o"字元的下標位置    alert(str.lastIndexOf(‘a‘));//擷取字串中最後一個"a"的下標位置    alert(str.indexOf(‘a‘,8));    alert(str.substring(3));//從第三位開始截取到最後的字串的值    alert(str.substring(3,5));//從第三位截取到第五位的字串    alert(str.slice(4));//截取從第四位到最後的字串    alert(str.replace(‘a‘,‘w‘));//將字串中的a替換為w    alert(str.match(‘v‘));//搜尋字串中的字元v    alert(str.search(‘love‘));//搜尋字串中字串的索引位置    document.write(str);    var a="5.5";    var b=2;    alert(a-b);    var e="lo"    var s="ve"    alert(e+s);    //總結:1.-*/只有運算功能    // 2.+ 執行運算功能:數實值型別+數實值型別    //3.+執行字串拼接功能:字串類型(只要有一個)+數實值型別    </script></head><body></body></html>
字串常用操作

 

JS基礎-資料類型轉化和字串常用操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.