[TOC]
# 題1、如何判斷一個字符中是否包含某一個字符?
答:
方法一、 indexOf,返回值是字符在字符串的位置(數字),如果在第1個位置返回0,如果沒有返回-1
方法二、includes(ES6),返回值是布爾類型
方法三、正則表達式,比如:判斷是否有x : 'abc'.match(/x/)
方法四、search,用于檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串。如果沒有找到任何匹配的字符串,則返回 -1
~~~
// 判斷一個字符串中是否包含某一個字符的方法
// 方法一、 indexOf() 返回值是字符在字符串中位置的下標,如果在第5個位置返回4,如果沒有返回 -1
console.log(a.indexOf('o')); ? ?// 4
// 方法二、 includes()(ES6),返回值是布爾類型
console.log(a.includes('o')) ? ?// true
// 方法三、 正則表達式,match()方法
console.log(a.match(/o/)); ? ? ?// [ 'o', index: 4, input: 'helloworld', groups: undefined ]
// 方法四、 search() 方法,用于檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串。如果沒有找到任何匹配的字符串,則返回 -1
console.log(a.search('o')); ? ? // 4
console.log(a.search(/o/)); ? ? // 4
?
~~~
# 題2、如何截取字符串中3~5個字符?
答:比如: 'helloworld' --> 截取3~5的結果:llo。
方法一、 substring, 'helloworld'.substring(2,5)
方法二、substr, 'helloworld'.substr(2,3)
區別: 第二個參數不同,一個是結束的下標(substring),一個是截取的長度(substr)
~~~
// 截取字符串中第3~5個字符
var b = 'helloworld'
// 方法一、 substring
console.log(b.substring(2, 5));
// 方法二、 substr
console.log(b.substr(2, 3));
~~~
# 題3、如何獲取一個字符串的長度?
答: length 屬性。比如,'helloworld'.length
# 題4、如果把字符串中的字母轉成大寫?轉成小寫呢?
答:
toUpperCase : 轉大寫
toLowerCase : 轉小寫
~~~
'abc'.toUpperCase() // ABC
'BCA'.toLowerCase() // bca
~~~
# 題5、 JS 中如何將單詞的首字母大寫?
答:
1. 先為字符串原型添加一個方法,在 String 原型上添加(String.prototype)的函數,可以在所有字符串上直接使用。
~~~
String.prototype.firstUpperCase = function(){
? ?// 通過正則表達式,匹配第一個字符,和后面的字符
? ?return this.replace(/\b(\w)(\w*)/g, function($0, $1, $2) {
? ? ? ?// 將第一個字符轉大寫 + 后面的字符轉小轉
? ? ? ?return $1.toUpperCase() + $2.toLowerCase();
? });
}
~~~
2. 添加完之后,所以的字符串就可以直接使用 firstUpperCase 方法了
~~~
'hello'.firstUpperCase() // Hello
~~~
# 題6、如何把字符串中所有的數字轉成 \* ?
答: 比如, 'a123b456c789' --> `a***b***c***`
實現思路: 使用這種表達式 + replace 方法
~~~
// 把字符串中所有的數字轉成 *
function replaceXX(str) {
? ?// 替換: 第一個參數:正則表達式
? ?// \\d: 數字
? ?// /g: 全局替換,如果不加只會替換第一個數字
? ?return str.replace(/\d/g,'*')
}
console.log(replaceXX('a123b456c789')); ?// a***b***c***
~~~
# 題7、如何將字符串轉成數組?
答: 使用 split 。
比如:
~~~
console.log('hi, nice, to, meet, you'.split(',')); ?// [ 'hi', ' nice', ' to', ' meet', ' you' ]
~~~
# 題8、如何將數組轉成字符串?
答:
方法一、 join , 優點:可以任意指定分隔符。
方法二、 toString , 默認是以 , 分隔
~~~
// 將數組轉成字符串
let arr = [ 'hi', ' nice', ' to', ' meet', ' you' ];
console.log(arr.join('')); ?// hi nice to meet you
console.log(arr.toString()); ? ?// hi, nice, to, meet, you
~~~
# 題9、如何合并兩個字符串?
答:
方法一、 使用 + ,比如: 'abc'+'bcd' ==> abcbcd
方法二、使用 concat方法,比如: 'abc'.concat('bcd') ==> abcbcd
~~~
// 合并兩個字符串
// 方法一、 +
console.log('abc' + 'bcd'); ? // abcbcd
// 方法二、 concat
let str = 'abc'.concat('bcd')
console.log(str); // abcbcd
~~~
# 題10、請說出至少 10 個操作字符串的方法?
答:
split: 字符串轉數組
join: 數組轉字符串
toString: 數組轉字符串
concat: 合并多個字符串
substring: 從開始到結束下標截取
substr: 從開始截取一定長度的字符串
indexOf: 查看一個字符串中某一個字符串的位置
replace: 字符串替換,可以使用正則來匹配替換
match: 用字符串去匹配一個正則
toUpperCase: 字母轉大寫
toLowerCase: 字母轉小寫

# 題11、 如何把一個字符串中所有的手機號碼中的中間4位數字變成 `****` ?
答:'ab13455334444sd' ==> 'ab1344444d'
實現思路: 使用正則表達式 + replace 方法
~~~
// 把一個字符串中所有的手機號碼中的中間4位數字變成 ****
function replaceXX(str) {
? ?return str.replace(/(1\d{2})(\d{4})(\d{4})/g, '$1****$3')
}
let rstr = 'abdf13344445555das'
console.log(replaceXX(rstr)) ? ?// abdf133****5555das
~~~
# 題12、 轉成駝峰命名法?
答: 比如:"get-element-by-id" --> getElementById
思路: 先轉數組,循環數組處理每個單詞首字母大寫,轉回字符串。
~~~
String.prototype.firstUpperCase = function(){
? ?// 通過正則表達式,匹配第一個字符,和后面的字符
? ?return this.replace(/\b(\w)(\w*)/g, function($0, $1, $2) {
? ? ? ?// 將第一個字符轉大寫 + 后面的字符轉小轉
? ? ? ?return $1.toUpperCase() + $2.toLowerCase();
? });
}
// 轉駝峰命名法
let hump = 'get-element-by-id'
hump = hump.split('-').map((v, k) => {
? ?if (k > 0) {
? ? ? ?return v.firstUpperCase()
? } else {
? ? ? ?return v
? }
}).join('')
console.log(hump); // getElementById
~~~