# str.replaceWhitespaces()

{% tabs %}
{% tab title="💾 程式" %}
:floppy\_disk: replit: [str.replaceWhitespaces()](https://replit.com/@pegasusroe/strreplaceWhitespaces#index.js)

```javascript
// 🔸 str.replaceWhitespaces(delimiter = ',')
String.prototype.replaceWhitespaces = function(delimiter = ',') {
    return this
        .trim()                        // ⭐️ removes whitespace from both ends
        .replace(/\s+/g, delimiter);
};
```

💈範例：

```javascript
// main
const str = `語文(國語文)_60100	節數_語文(國語文)_60100	權數_語文(國語文)_60100	語文(英語文)_60501	節數_語文(英語文)_60501	權數_語文(英語文)_60501	社會(歷史)_60801	節數_社會(歷史)_60801	權數_社會(歷史)_60801	社會(地理)_60802	節數_社會(地理)_60802	權數_社會(地理)_60802	社會(公民)_60803	節數_社會(公民)_60803	權數_社會(公民)_60803	數學_61000	節數_數學_61000	權數_數學_61000	健康與體育(健康教育)_61101	節數_健康與體育(健康教育)_61101	權數_健康與體育(健康教育)_61101	健康與體育(體育)_61102	節數_健康與體育(體育)_61102	權數_健康與體育(體育)_61102	綜合活動(輔導活動)_61301	節數_綜合活動(輔導活動)_61301	權數_綜合活動(輔導活動)_61301	綜合活動(童軍教育)_61302	節數_綜合活動(童軍教育)_61302	權數_綜合活動(童軍教育)_61302	綜合活動(家政實習)_61304	節數_綜合活動(家政實習)_61304	權數_綜合活動(家政實習)_61304	專長訓練_61407	節數_專長訓練_61407	權數_專長訓練_61407	藝術(視覺藝術)_61601	節數_藝術(視覺藝術)_61601	權數_藝術(視覺藝術)_61601	藝術(表演藝術)_61602	節數_藝術(表演藝術)_61602	權數_藝術(表演藝術)_61602	藝術(音樂)_61603	節數_藝術(音樂)_61603	權數_藝術(音樂)_61603	自然科學(理化)_61701	節數_自然科學(理化)_61701	權數_自然科學(理化)_61701	科技(資訊科技)_61801	節數_科技(資訊科技)_61801	權數_科技(資訊科技)_61801	科技(生活科技)_61802	節數_科技(生活科技)_61802	權數_科技(生活科技)_61802`;
const a = str.replaceWhitespaces().split(',');

a                   // [ '語文(國語文)_60100', ... ]
a.length            // 54
a.length / 3        // 18
```

{% endtab %}

{% tab title="⬇️ 應用" %}

* [split-into-course-names](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.match/split-into-course-names "mention")
* [no-make-up-exam](https://lochiwei.gitbook.io/web/appendix/gas/projects/no-make-up-exam "mention")
* [class-average](https://lochiwei.gitbook.io/web/appendix/gas/projects/class-average "mention")
  {% endtab %}

{% tab title="🗣 討論" %}

* [How to remove spaces from a string using JavaScript?](https://stackoverflow.com/a/5963202/5409815)
  {% endtab %}

{% tab title="👥 相關" %}

* [template-literal](https://lochiwei.gitbook.io/web/js/val/prim/str/template-literal "mention") - support multi-line strings.
* [regex](https://lochiwei.gitbook.io/web/js/val/builtin/regex "mention")
* [str.replace](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.replace "mention")
* [arr.filter](https://lochiwei.gitbook.io/web/js/val/builtin/arr/method/arr.filter "mention")
* [arr.removevalue](https://lochiwei.gitbook.io/web/js/val/builtin/arr/ext/arr.removevalue "mention")
* [str.removewhitespaces](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.removewhitespaces "mention") - remove all whitespaces in string.
* 常用來淨化 [gas](https://lochiwei.gitbook.io/web/appendix/gas "mention") 中的 [app](https://lochiwei.gitbook.io/web/appendix/gas/app "mention") 儲存格的值。
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.replacewhitespaces.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
