# String

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [value](https://lochiwei.gitbook.io/web/js/val) ⟩ [primitive](https://lochiwei.gitbook.io/web/js/val/prim) ⟩ String

{% hint style="success" %}

* JavaScript <mark style="color:purple;">**strings**</mark> are encoded as a sequence of <mark style="color:yellow;">**16-bit**</mark> [**code units**](https://lochiwei.gitbook.io/web/js/val/prim/str/unicode/encode/utf16/code-unit).&#x20;
  {% endhint %}

{% tabs %}
{% tab title="⭐️" %}
{% hint style="info" %} <mark style="color:purple;">**Strings**</mark>

* are [iterable](https://lochiwei.gitbook.io/web/js/iteration/iterable "mention").
  {% endhint %}

{% hint style="success" %}
[iterator](https://lochiwei.gitbook.io/web/js/iteration/iterator "mention") for [str](https://lochiwei.gitbook.io/web/js/val/prim/str "mention") works <mark style="color:green;">**correctly**</mark> with <mark style="color:yellow;">**surrogate pairs**</mark>. \
👉 see： [str.slice2](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.slice2 "mention")
{% endhint %}

{% hint style="success" %} <mark style="color:red;">**Any**</mark> [<mark style="color:yellow;">**non-nullish**</mark>](https://lochiwei.gitbook.io/web/js/val/prim/nullish) value has **.**<mark style="color:blue;">**toString**</mark>**()** method. (usually <mark style="color:yellow;">**equivalent**</mark> to <mark style="color:purple;">**String**</mark>**()**)
{% endhint %}
{% endtab %}

{% tab title="🔴" %}

* [instance methods](https://lochiwei.gitbook.io/web/js/val/prim/str/method)
* [with-string](https://lochiwei.gitbook.io/web/js/grammar/statement/loop/for/of/with-string "mention")
* [template-literal](https://lochiwei.gitbook.io/web/js/val/prim/str/template-literal "mention")
* [tagged-template](https://lochiwei.gitbook.io/web/js/val/prim/str/template-literal/tagged-template "mention")
* [vs-string](https://lochiwei.gitbook.io/web/js/grammar/token/id/vs-string "mention")
  {% endtab %}

{% tab title="👥" %}

* [randomInt()](https://lochiwei.gitbook.io/web/js/val/prim/num/random/randomint)
* <mark style="color:purple;">**String**</mark>
  * is [iterable](https://lochiwei.gitbook.io/web/js/iteration/iterable "mention").
  * is [array-like](https://lochiwei.gitbook.io/web/js/val/builtin/arr/array-like "mention").
  * is used in [bracket-notation](https://lochiwei.gitbook.io/web/js/val/obj/prop/access/bracket-notation "mention").
    {% endtab %}

{% tab title="💾" %}

```javascript
// ⭐️ 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('');
};
```

{% endtab %}

{% tab title="🔸" %}
instance methods:

* [repeat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat) - 重複字串：<mark style="color:blue;">`'_'.repeat(27)`</mark>
* [padStart()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart) - 字串前方補字
* [padEnd()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd)

custom methods:

* [escapeHTML()](https://lochiwei.gitbook.io/web/js/val/prim/str/method/escapehtml)
* [random()](https://lochiwei.gitbook.io/web/js/val/prim/str/method/.random)
* [shuffle()](https://lochiwei.gitbook.io/web/js/val/prim/str/method/string.shuffle)
* [str.slice2](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.slice2 "mention") - slice strings with uncode correctly.

custom static methods:

* [String.fromCharCodesBetween()](https://lochiwei.gitbook.io/web/js/val/prim/str/method/string.fromcharcodesbetween)
  {% endtab %}

{% tab title="📗" %}

* 👉 [random password](https://lochiwei.gitbook.io/web/appendix/custom/custom-functions/random-password)
* 👉 [Array 常用函數](https://lochiwei.gitbook.io/web/js/val/builtin/arr)
* Eloquent JavaScript ⟩ [Strings & Character Codes](https://eloquentjavascript.net/05_higher_order.html#h_gQf5HZNGpM)
  {% endtab %}

{% tab title="📘" %}

* [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)
  {% endtab %}

{% tab title="✨ 範例" %}

* [randomPassword()](https://lochiwei.gitbook.io/web/appendix/custom/custom-functions/random-password)
* [with-string](https://lochiwei.gitbook.io/web/js/grammar/statement/loop/for/of/with-string "mention")
  {% endtab %}
  {% endtabs %}
