字串的方法

2021-09-25 02:56:41 字數 1607 閱讀 1980

str = "hello atguigu";

str2 = "中hello atguigu"

console.log(str.charat(0)) // 根據索引獲取指定的字元

console.log(str2.charcodeat(1)) //獲取索引處的字元編碼(unicode編碼)

console.log(string.fromcharcode(72)) //根據字元編碼,獲取字元

console.log(str.concat("你好", "再見")) //拼接字串

console.log(str.indexof("h", 1)) // -1

// 該方法可以檢索乙個字串中是否含有指定內容

// 如果字串中含有該內容,則會返回其第一次出現的索引

// 如果沒有找到指定的內容,則返回 - 1

// 可以指定乙個第二個引數,指定開始查詢的位置

console.log(str.lastindexof('a')) //-1

// 該方法的用法和indexof() 一樣,

// 不同的是indexof是從前往後找,

// 而lastindexof是從後往前找

// 也可以指定開始查詢的位置

console.log(str.slice(1, 4)) // ell

// -可以從字串中擷取指定的內容

// -不會影響原字串, 而是將擷取到內容返回

// -引數:

// 第乙個, 開始位置的索引( 包括開始位置)

// 第二個, 結束位置的索引( 不包括結束位置)

// -如果省略第二個引數, 則會擷取到後邊所有的

// -也可以傳遞乙個負數作為引數, 負數的話將會從後邊計算

console.log(str.substring(0, 1)) // h

// 可以用來擷取乙個字串, 可以slice() 類似

// 引數:

// 第乙個: 開始擷取位置的索引( 包括開始位置)

// 第二個: 結束位置的索引( 不包括結束位置)

// 不同的是這個方法不能接受負值作為引數,

// 如果傳遞了乙個負值, 則預設使用0

// 而且他還自動調整引數的位置, 如果第二個引數小於第乙個, 則自動交換

console.log(str.substr(3, 2)) // lo

// 用來擷取字串

// 引數

// 1.擷取開始位置的索引

// 2.擷取的長度

console.log(str.split("u")) // ["hello atg","ig",""]

// 將乙個字串拆分為乙個陣列

// 需要乙個字串作為引數,將會根據該字串去拆分陣列

// 如果傳入乙個空串,則會將每個字元都拆分為陣列中的乙個元素

console.log(str.touppercase()) // hello atguigu

//將乙個字串轉換為大寫並返回

console.log(str.tolowercase()) // hello atguigu

//將乙個字串轉換為小寫並返回

字串及字串的方法

一 字串 js中的任何資料型別都可以當作物件來看。所以string既是基本資料型別,又是物件。二 宣告字串 var sstr 字串 var ostr new string 字串 三 字串屬性 1.length計算字串的長度 不區分中英文 var str hello world console.log...

字串的方法

獲取 int length 獲取長度 char charat int index 獲取位置上的某個字元 int indexof int ch 因為接受的是asc 碼,返回的是ch在字串中出現的第一次位置 int indexof int ch,int fromindex 從fromindex指定位置獲...

字串的方法

1.charat 下標 作用 查詢該下標位置處的字元,返回乙個字元 2.charcodeat 下標 作用 查詢該下標位置處的字元的unicode編碼 3.fromcharcode 作用 根據乙個unicode編碼值返回對應的字元,與charcodeat 結果相反 4.indexof 作用 查詢某個字...