亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

在JAVAScript中提供了一組豐富的方法來操作和處理字符串。在這篇文章中,我將向您介紹13個最常用的JavaScript字符串方法及其功能。

String length

如果你想找到一個字符串中的字符數,那么你可以使用length屬性。

const str = "This is a string.";
const lengthOfStr = str.length;
console.log(lengthOfStr); // Output: 17

這也計算了空格的長度。

String toUpperCase()

如果你想把一個字符串轉換成大寫字符串,那么你可以使用toUpperCase()方法。

const str = "This is a string.";
const uppercaseStr = str.toUpperCase();
console.log(uppercaseStr); // Output: THIS IS A STRING.

String toLowerCase()

如果你想把一個字符串轉換成小寫字符串,那么你可以使用toLowerCase()方法。

const str = "This Is a String.";
const lowercaseStr = str.toLowerCase();
console.log(lowercaseStr); // Output: this is a string.

String indexOf()

如果你想在一個字符串中找到一個子字符的第一次出現位置,那么你可以使用indexOf()方法。

const str = "This is a js string and js string is nice.";
const indexOfJs = str.indexOf("js");
console.log(indexOfJs); // Output: 10

String lastIndexOf()

如果你想在一個字符串中找到一個子字符的最后一次出現,那么你可以使用lastIndexOf()方法。

const str = "This is a js string and js string is nice.";
const lastIndexOfJs = str.lastIndexOf("js");
console.log(lastIndexOfJs); // Output: 24

String slice()

如果你想提取字符串的一部分,那么你可以使用slice()方法。這將以新字符串的形式返回提取的部分。

語法:

string.slice(start position, end position);

將不包括結束位置。

//Example:1
const str1 = "This is a string.";
const slicedStr1 = str1.slice(0, 7);
console.log(slicedStr1); // Output: This is

//Example:2
const str2 = "This is a string.";
const slicedStr2 = str2.slice(3, 9);
console.log(slicedStr2); // Output: s is a

如果你沒有指定結束位置,那么這將切出字符串的其余部分。

const str = "This is a string.";
const slicedStr = str.slice(5);
console.log(slicedStr); // Output: is a string.

也可以給予參數為負數。

const str = "This is a string.";
const slicedStr = str.slice(-3, -1);
console.log(slicedStr); // Output: ng

簡單一點你可以這樣理解負數為參考的情況:

str.slice(-3, -1);
str.slice(str.length-3, str.length-1);
str.slice(17-3, 17-1);
str.slice(14, 16);

String substring()

substring()方法類似于slice()方法,但不同的是,如果你給它負參數,那么它們將被視為0。

const str = "This is a string.";
const slicedStr = str.substring(-3, 5);
console.log(slicedStr); // Output: This

String substr()

substr()方法類似于slice()方法,但不同之處在于end參數是要提取的字符的長度。

const str = "This is a string.";
// 這里代表從索引11開始提取4個字符
const slicedStr = str.substr(11, 4); 
console.log(slicedStr); // Output: trin

String charAt()

如果你想在一個字符串中獲得一個指定索引的字符,那么你可以使用charAt()方法。

const str = "This is a string.";
const character = str.charAt(13);
console.log(character); // Output: i

String concat()

如果你想連接兩個或多個字符串,那么你可以使用concat()方法。

const firstName = "John";
const lastName = "Doe";
const fullName = firstName.concat(" ", lastName);
console.log(fullName); // Output: John Doe

String trim()

您可以使用trim()方法從字符串的兩端刪除空格字符。

const str = "    This is a string.    ";
const trimmedStr = str.trim();
console.log(trimmedStr); // Output: This is a string.

String replace()

如果你想用另一個字符串替換一個指定的子字符串,那么你可以使用replace()方法。

const str = "JavaScript is amazing!";
const replacedStr = str.replace("amazing", "awesome");
console.log(replacedStr); // Output: JavaScript is awesome!

String split()

你可以使用split()方法將字符串轉換為數組。

const str1 = "JavaScript is amazing!";
const arr1 = str1.split();
console.log(arr1); // Output: ['JavaScript is amazing!']

//Example:2
const str2 = "JavaScript is amazing!";
const arr2 = str2.split(" ");
console.log(arr2); // Output: ['JavaScript', 'is', 'amazing!']

分享到:
標簽:JavaScript
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定