> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/prim/str.md).

# String

🚧 under construction -> move 💾 程式

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [primitive](/web/js/val/prim.md) ⟩ 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**](/web/js/val/prim/str/unicode/encode/utf16/code-unit.md).&#x20;
  {% endhint %}

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

* are [iterable](/web/js/iteration/iterable.md).
  {% endhint %}

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

{% hint style="success" %} <mark style="color:red;">**Any**</mark> [<mark style="color:yellow;">**non-nullish**</mark>](/web/js/val/prim/nullish.md) 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](/web/js/val/prim/str/method.md)
* [for-of with strings](/web/js/grammar/statement/loop/for/of/with-string.md)
* [template literal](/web/js/val/prim/str/template-literal.md)
* [tagged template](/web/js/val/prim/str/template-literal/tagged-template.md)
* [identifier vs. string](/web/js/grammar/token/id/vs-string.md)
  {% endtab %}

{% tab title="👥" %}

* [randomInt()](/web/js/val/prim/num/random/randomint.md)
* <mark style="color:purple;">**String**</mark>
  * is [iterable](/web/js/iteration/iterable.md).
  * is [array-like](/web/js/val/builtin/arr/array-like.md).
  * is used in [bracket notation \[\]](/web/js/val/obj/prop/access/bracket-notation.md).
    {% 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()](/web/js/val/prim/str/method/escapehtml.md)
* [random()](/web/js/val/prim/str/method/.random.md)
* [shuffle()](/web/js/val/prim/str/method/string.shuffle.md)
* [str.slice2()](/web/js/val/prim/str/method/str.slice2.md) - slice strings with uncode correctly.

custom static methods:

* [String.fromCharCodesBetween()](/web/js/val/prim/str/method/string.fromcharcodesbetween.md)
  {% endtab %}

{% tab title="📗" %}

* 👉 [random password](/web/appendix/custom/custom-functions/random-password.md)
* 👉 [Array 常用函數](/web/js/val/builtin/arr.md)
* 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()](/web/appendix/custom/custom-functions/random-password.md)
* [for-of with strings](/web/js/grammar/statement/loop/for/of/with-string.md)
  {% endtab %}
  {% endtabs %}
