DIV CSS 佈局教程網

JavaScript substring() 方法介紹與範例
編輯:JavaScript綜合知識     

方法用途

substring() 會傳回字串中介於兩個索引之間的子字串。它不會修改原字串,且若起始索引大於結束索引,會自動交換兩個參數。

語法

string.substring(indexA, indexB)
  • indexA:起始位置。
  • indexB:結束位置,可省略。

範例

const str = 'Apples are round, and apples are juicy.';

console.log(str.substring(1, 2));
console.log(str.substring(0, 10));
console.log(str.substring(5));

輸出分別為「p」、「Apples are」與「s are round, and apples are juicy.」。若傳入負數,會被視為 0。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved