🔤String
🚧 under construction -> move 💾 程式
JS ⟩ value ⟩ primitive ⟩ String
JavaScript strings are encoded as a sequence of 16-bit code units.
iterator for String works correctly with surrogate pairs. 👉 see: str.slice2()
Any non-nullish value has .toString() method. (usually equivalent to String())
String
is iterable.
is array-like.
is used in bracket notation [].
// ⭐️ string.random()
// ⭐️ Note: this may not work well with Unicode❗️
String.prototype.random = function(){
return this[randomInt(this.length)];
}
// ⭐️ string.shuffle()
String.prototype.shuffle = function(){
var a = this.split("");
var n = a.length;
for(var i = n - 1; i > 0; i--) {
var j = randomInt(i+1);
[a[i], a[j]] = [a[j], a[i]];
}
return a.join("");
}
// ⭐️ String.fromCharCodesBetween(65, 70) -> 'ABCDEF'
String.fromCharCodesBetween = function(from, to){
let array = [];
for(let i = from; i <= to; i++) array.push(i);
return array
.map(code => String.fromCharCode(code))
.join('');
};instance methods:
repeat() - 重複字串:
'_'.repeat(27)padStart() - 字串前方補字
custom methods:
str.slice2() - slice strings with uncode correctly.
custom static methods:
Eloquent JavaScript ⟩ Strings & Character Codes
Last updated
Was this helpful?