Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

要有這個概念- 字串就是字元組成的陣列 #5

Open
chuchushooes opened this issue Sep 20, 2022 · 0 comments
Open

要有這個概念- 字串就是字元組成的陣列 #5

chuchushooes opened this issue Sep 20, 2022 · 0 comments
Labels
JS Improvements or additions to documentation

Comments

@chuchushooes
Copy link
Owner

chuchushooes commented Sep 20, 2022

原文網址

每個字串有自己的索引值 (index),也就是說,字串是由字元組成的陣列。

Link:字串的陣列定義,在 MDN 內字串解說

有兩個方法可以存取字串中個別的字元。
第一個是用 charAt (en-US) 方法: return 'cat'.charAt(1) // 回傳 "a"

return 'cat'.charAt(1); // 回傳 "a"

另一個( 在 ECMAScript 5 中被提到 )方法是將字串當作一個類似陣列的物件,直接存取字串中對應的數值索引。

return 'cat'[1]; // 回傳 "a"


如何查找索引位置? 用 indexOf() 找

indexOf() — 查找索引位置
Link:MDN indexOf

indexOf()方法會回傳給定元素於陣列中第一個被找到之索引,
若不存在於陣列中則回傳 -1。

如下示範

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'] 
console.log(beasts.indexOf('bison')) // output: 1 , bison位置在1 
console.log(beasts.indexOf('bison', 2)) 
// output: 4 ,從 index 2 開始找, bison位置在4 
console.log(beasts.indexOf('giraffe')) // output: -1 , 沒有giraffe

indexOf() 語法

arr.indexOf(searchElement[, fromIndex])

indexOf() 參數 (每個位置語法的意義)

searchElement欲在陣列中搜尋的元素。

fromIndex 選擇性陣列中搜尋的起始索引。

若這個索引值大於或等於陣列長度,會直接回傳 -1,意即不會在陣列中搜尋。 如果索引值是一個負數,會從陣列的最後一個往回算,最後一個的索引值為 -1,以此類推。

如下示範

//由ant開始找 
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']
console.log(beasts.indexOf('bison', -5))
/*  
output: 1,  index -5 位置為 ant 開始找, 由左往右找 index 1 為 bison
*/ 
--------------------------------------------------------------- 
//由duck開始找 
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']
console.log(beasts.indexOf('bison', -2))
/*  
output: 4,  index -2 位置為 duck 開始找, 由左往右找 index 4 為 bison 
*/

注意:儘管往回算,但依然會從左往右全部搜尋。
如果負數索引值在回頭計算之後仍然小於 0,則會從左往右全部搜尋。
這個參數的預設值為 0(即搜尋整個陣列)。

indexOf() 回傳值
在陣列中找到的第一個元素索引值;沒找到則為 -1。


其他字串的方法應用

trim() — 移除空白

trim()方法會移除字串起始及結尾處的空白字元。
Link:MDN trim

本文中的空白字元指所有空格字元(如:空格、欄標、無間斷空格等等)及換行字元(如:換行、回車等等)。

以下示範

const greeting = '   Hello world!   '
console.log(greeting);
// expected output: "   Hello world!   "
console.log(greeting.trim()); // 後面加上 trim 即可把空白移除 //
expected output: "Hello world!"

trim 語法
trim()

trim 回傳值解釋
回傳一個新的字串,其為把 str 起始及結尾處的空白字元移除後的值。

如果 str 的起始及結尾處沒有任何的空白字元,此方法不會拋出任何例外,且仍然會回傳一個新的字串(本質上為 str 的複製)。


split() — 將字串分割成陣列
split() 方法使用的分隔符字符串將一個String對象分割成子字符串,以一個指定的分割字符串來決定最大的分離位置。
Link:MDN split

● 用,分割陣列

const myString = 'hello, nihao, 你好' 
const myNewArray = myString.split(',') // 用,分割陣列 
console.log(myNewArray) // ['hello', ' nihao', ' 你好']

● 用無空格來分割陣列

const str = 'The quick brown fox jumps over the lazy dog.' 
const chars = str.split('') // 無空格來切割 console.log(chars) 
/* 
output: 
[   'T', 'h', 'e', ' ', 'q', 'u', 'i',   'c', 'k', ' ', 'b', 'r', 'o', 'w',   'n', ' ', 'f', 'o', 'x', ' ', 'j',   'u', 'm', 'p', 's', ' ', 'o', 'v',   'e', 'r', ' ', 't', 'h', 'e', ' ',   'l', 'a', 'z', 'y', ' ', 'd', 'o',   'g', '.' ] 
*/

以上介紹了 3 種與字串有關的應用,要記得字串就是字元組成的陣列

● indexOf() — 查找索引位置:indexOf()方法會回傳給定元素於陣列中第一個被找到之索引,若不存在於陣列中則回傳 -1。
Link:MDN indexOf

● trim()—移除空白:trim()方法會移除字串起始及結尾處的空白字元。
本文中的空白字元指所有空格字元(如:空格、欄標、無間斷空格等等)及換行字元(如:換行、回車等等)。
Link:MDN trim

● split() — 將字串分割成陣列:split() 方法使用的分隔符字符串將一個String對象分割成子字符串,以一個指定的分割字符串來決定最大的分離位置。
Link:MDN split

@chuchushooes chuchushooes added the JS Improvements or additions to documentation label Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JS Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant