av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

創(chuàng)新互聯(lián)JAVA教程:JavaScript 字符串(String)對象

 String 對象用于處理已有的字符塊。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供硯山網(wǎng)站建設(shè)、硯山做網(wǎng)站、硯山網(wǎng)站設(shè)計(jì)、硯山網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、硯山企業(yè)網(wǎng)站模板建站服務(wù),十年硯山做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。


JavaScript 字符串

一個(gè)字符串用于存儲一系列字符就像 "John Doe".

一個(gè)字符串可以使用單引號或雙引號:

實(shí)例

var carname="Volvo XC60";

var carname='Volvo XC60';

你使用位置(索引)可以訪問字符串中任何的字符:

實(shí)例

var character=carname[7];

字符串的索引從零開始, 所以字符串第一字符為 [0],第二個(gè)字符為 [1], 等等。

你可以在字符串中使用引號,如下實(shí)例:

實(shí)例

var answer="It's alright";

var answer="He is called 'Johnny'";

var answer='He is called "Johnny"';

或者你可以在字符串中使用轉(zhuǎn)義字符(\)使用引號:

實(shí)例

var answer='It\'s alright';

var answer="He is called \"Johnny\"";

嘗試一下 ?


字符串(String)

字符串(String)使用長度屬性length來計(jì)算字符串的長度:

實(shí)例

var txt="Hello World!";

document.write(txt.length);

var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

document.write(txt.length);

嘗試一下 ?

JavaScript 獲取字符串的長度:通過在字符串變量或字符串后面寫上 .length 來獲得變量中string (字符串)值的長度。


在字符串中查找字符串

字符串使用 indexOf() 來定位字符串中某一個(gè)指定的字符首次出現(xiàn)的位置:

實(shí)例

var str="Hello world, welcome to the universe.";

var n=str.indexOf("welcome");

嘗試一下 ?

如果沒找到對應(yīng)的字符函數(shù)返回-1

lastIndexOf() 方法在字符串末尾開始查找字符串出現(xiàn)的位置。


內(nèi)容匹配

match()函數(shù)用來查找字符串中特定的字符,并且如果找到的話,則返回這個(gè)字符。

實(shí)例

var str="Hello world!";

document.write(str.match("world") + "
");

document.write(str.match("World") + "
");

document.write(str.match("world!"));

嘗試一下 ?


替換內(nèi)容

replace() 方法在字符串中用某些字符替換另一些字符。

實(shí)例

str="Please visit Microsoft!"

var n=str.replace("Microsoft","CDCXHL.COM");

嘗試一下 ?


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

字符串大小寫轉(zhuǎn)換使用函數(shù) toUpperCase() / toLowerCase():

實(shí)例

var txt="Hello World!";       // String

var txt1=txt.toUpperCase();   // txt1 is txt converted to upper

var txt2=txt.toLowerCase();   // txt2 is txt converted to lower

嘗試一下 ?


字符串轉(zhuǎn)為數(shù)組

字符串使用string>split()函數(shù)轉(zhuǎn)為數(shù)組:

實(shí)例

txt="a,b,c,d,e"   // String

txt.split(",");   // Split on commas

txt.split(" ");   // Split on spaces

txt.split("|");   // Split on pipe 

嘗試一下 ?


特殊字符

Javascript 中可以使用反斜線(\)插入特殊符號,如:撇號,引號等其他特殊符號。

查看如下 JavaScript 代碼:

var txt="We are the so-called "Vikings" from the north.";
document.write(txt);

在JavaScript中,字符串的開始和停止使用單引號或雙引號。這意味著,上面的字符串將被切成: We are the so-called

解決以上的問題可以使用反斜線來轉(zhuǎn)義引號:

var txt="We are the so-called \"Vikings\" from the north.";
document.write(txt);

JavaScript將輸出正確的文本字符串:We are the so-called "Vikings" from the north.

下表列出其他特殊字符,可以使用反斜線轉(zhuǎn)義特殊字符:

代碼輸出
\'單引號
\"雙引號
\\斜桿
\n換行
\r回車
\ttab
\b空格
\f換頁


字符串屬性和方法

屬性:

  • length
  • prototype
  • constructor

方法:

  • charAt()
  • charCodeAt()
  • concat()
  • fromCharCode()
  • indexOf()
  • lastIndexOf()
  • match()
  • replace()
  • search()
  • slice()
  • split()
  • substr()
  • substring()
  • toLowerCase()
  • toUpperCase()
  • valueOf()

相關(guān)文章

JavaScript 和 HTML DOM 參考手冊:JavaScript String 對象


JavaScript 字符串函數(shù)學(xué)習(xí)腦圖


文章標(biāo)題:創(chuàng)新互聯(lián)JAVA教程:JavaScript 字符串(String)對象
轉(zhuǎn)載來源:http://uogjgqi.cn/article/cdepjhc.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流