自己做做看。
String.repeat
1 2 3 4 5 6 7 8
| function repeat(str, times) { var result = '' for(var i=0; i<times; i++) { result += str } return result } console.log(repeat('a', 5))
|
String.trim
解法 1 => 找出頭尾的索引值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| function trim(s) { let result = '' let first = 0 let last = s.length - 1
for(let i=0; i<s.length; i++) { if(s[i] !== ' ') { first = i break } } for(let i=s.length-1; i>=0; i--) { if(s[i] !== ' ') { last = i break } } for(let i=first; i<=last; i++) { result += s[i] } return result } console.log(trim(' abc ')) console.log(trim(' a b c ')) console.log(trim(' abc')) console.log(trim('abc '))
|
解法 2 => 先去掉前面的空白,再去掉後面的空白:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| function trim(s) { let isFrontWhiteSpaceFounded = false let isBackWhiteSpaceFounded = false let front = '' let back = '' for(let i=0; i<s.length; i++) { if(s[i] !== ' ' || isFrontWhiteSpaceFounded) { front += s[i] isFrontWhiteSpaceFounded = true } } for(let i=front.length-1; i>=0; i--) { if(front[i] !== ' ' || isBackWhiteSpaceFounded) { back = front[i] + back isBackWhiteSpaceFounded = true } } return back } console.log(trim(' abc ')) console.log(trim(' a b c ')) console.log(trim(' abc')) console.log(trim('abc '))
|
String.toLowerCase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| function toLowerCase(s) { let result = '' for(let i=0; i<s.length; i++) { if(s[i]>='A' && s[i]<='Z') { result += String.fromCharCode(s[i].charCodeAt(0) + 32) } else { result += s[i] } } return result } console.log(toLowerCase('ZZVa asS a C AsABC!!!!')) console.log(toLowerCase('!!!!')) console.log(toLowerCase(' '))
|
String.endsWith
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| function endsWith(str, searchString) { if(searchString.length > str.length || searchString.length === 0) return false let k = searchString.length - 1 for(let i=str.length-1; i>=0; i--) { if(str[i] !== searchString[k--]) return false if(k < 0) break } return true } console.log(endsWith('abc', 'c')) console.log(endsWith('abc', 'bc')) console.log(endsWith('abc', 'abc')) console.log(endsWith('abc', 'aaabc')) console.log(endsWith('abc', ''))
|
String.padEnd
解法 1 => 直接把填充文字填入,最後再把多餘的部分去掉:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| function padEnd(str, targetLength, padString=' ') { if( targetLength <= str.length || !(padString) ) return str let result = '' while(str.length < targetLength) { str += padString } for(let i=0; i<targetLength; i++) { result += str[i] } return result } console.log(padEnd('abc', 10, 'a')) console.log(padEnd('abc', 10, 'dog')) console.log(padEnd('abc', 2, 'abbc')) console.log(padEnd('abc', 2, '')) console.log(padEnd('abc', 5))
|
解法 2 => 把填充文字一個字一個字加入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| function padEnd(str, targetLength, padString=' ') { if( targetLength <= str.length || !(padString) ) return str let k = 0 while(str.length < targetLength) { str += padString[k++] if(k === padString.length) k=0 } return str } console.log(padEnd('abc', 10, 'a')) console.log(padEnd('abc', 10, 'dog')) console.log(padEnd('abc', 2, 'abbc')) console.log(padEnd('abc', 2, '')) console.log(padEnd('abc', 5))
|
String.slice
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function slice(str, start, end) { if(!end) end = str.length let result = '' for(let i=start; i<end; i++) { result += str[i] } return result } console.log(slice('abcdefg', 0, 3)) console.log(slice('abcdefg', 3, ))
|