JavaScript 字串方法

來源:互聯網
上載者:User

標籤:set   body   個數   基於   位置   使用   ring   理解   tin   

字元方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>字元方法</title>
</head>
<body>
<script type="text/javascript">
/*
charAt方法和charCodeAt方法都接收一個參數,基於0的字元位置
charAt方法是以單字元字串的形式返回給定位置的那個字元
charCodeAt方法擷取到的不是字元而是字元編碼
*/
var str="hello world";
console.log(str.charAt(1));//e
console.log(str.charCodeAt(1));//101
//還可以使用方括弧加數字索引來訪問字串中特定的字元
console.log(str[1]);//e
</script>
</body>
</html>
字串操作方法
concat方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>concat方法</title>
</head>
<body>
<script type="text/javascript">
var str="hello ";
var res=str.concat("world");
console.log(res);//hello world
console.log(str);//hello 這說明原來字串的值沒有改變
var res1=str.concat("nihao","!");
console.log(res1);//hello nihao! 說明concat方法可以接收任意多個參數
//雖然concat方法是專門用來拼接字串的,但是實踐中我們使用最多的還是加操作符+,因為其簡易便行
</script>
</body>
</html>
slice方法、substring方法、substr方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>字串操作方法</title>
</head>
<body>
<script type="text/javascript">
/*
slice方法:第一個參數指定子字串開始位置,第二個參數表示子字串最後一個字元後面的位置
substring方法:第一個參數指定子字串開始位置,第二個參數表示子字串最後一個字元後面的位置
substr方法:第一個參數指定子字串開始位置,第二個參數表示返回的字元個數
這三個方法都會返回被操作字串的一個子字串,都接收一或兩個參數
如果沒有給這些方法傳遞第二個參數,則將字串的長度作為結束位置。這些方法也不會修改字串本身,只是返回一個基本類型的字串值
*/
var str="hello world";
console.log(str.slice(3));//lo world
console.log(str.substring(3));//lo world
console.log(str.substr(3));//lo world
console.log(str.slice(3,7));//lo w 7表示子字串最後一個字元後面的位置 簡單理解就是包含頭不包含尾
console.log(str.substring(3,7));//lo w
console.log(str.substr(3,7));//lo worl 7表示返回7個字元

console.log(str.slice(3,-4));//lo w -4+11=7表示子字串最後一個字元後面的位置 簡單理解就是包含頭不包含尾
console.log(str.substring(3,-4));//hel 會轉換為console.log(str.substring(3,0));
//此外由於這個方法會將較小數作為開始位置,較大數作為結束位置,所以相當於調用console.log(str.substring(0,3));
console.log(str.substr(3,-4));//""Null 字元串
console.log(str.substring(3,0));
</script>
</body>
</html>
字串位置方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>字串位置方法</title>
</head>
<body>
<script type="text/javascript">
/*
indexOf方法和lastIndexOf方法都是從一個字串中搜尋給定的子字串,然後返回子字串的位置,如果沒有找到,則返回-1
indexOf方法是從字串的開頭向後搜尋子字串,lastIndexOf方法正好相反
這兩個方法都可以接收兩個參數:要尋找的子字串和尋找的位置
*/
var str="hello world";
console.log(str.indexOf("o"));//4
console.log(str.lastIndexOf("o"));//7
console.log(str.indexOf("o",6));//7
console.log(str.lastIndexOf("o",6));//4
</script>
</body>
</html>
trim方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>trim方法</title>
</head>
<body>
<script type="text/javascript">
/*
trim方法用來刪除字串前後的空格
*/
var str=" hello world ";
console.log(‘(‘+str.trim()+‘)‘);//(hello world)
console.log(‘(‘+str+‘)‘);//( hello world )
</script>
</body>
</html>

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.