# Proposal:String-toSpliced

**URL:** <https://es.discourse.group/t/proposal-string-tospliced/2142>\
**Category:** 🦋 Proposals\
**Created:** [September 14, 2024, 7:46am UTC](https://es.discourse.group/t/proposal-string-tospliced/2142 "2024-09-14T07:46:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![electroluxcode](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/electroluxcode/32/2222_2.png) [@electroluxcode](https://es.discourse.group/u/electroluxcode)\
**Post date:** [September 14, 2024, 7:46am UTC](https://es.discourse.group/t/proposal-string-tospliced/2142/1 "2024-09-14T07:46:18Z")

</div>

#### Proposal: [GitHub - electroluxcode/proposal-string-toSpliced: This proposal suggests adding a new method in JavaScript to allow for more convenient string manipulation patterns](https://github.com/electroluxcode/proposal-string-toSpliced)

Hi everyone,

I want to introduce a new proposal for making `toSpliced` statements for `String` more flexible in JavaScript. it similar to the `toSpliced` method of an array

here is some example

```js
let str1 = "Hello world";
let newStr1 = str1.toSpliced(5, 0, "beautiful ");
console.log(newStr1); // Outputs: Hello beautiful world

let str3 = "JavaScript is great";
let newStr3 = str3.toSpliced(10, 5);
console.log(newStr3); // Outputs: JavaScript is

```

#### Motivation

in my development work, I often deal with rich text and use the APIs of highlight and range, which provide two properties: startOffset and endOffset. When implementing the function of replacing search highlights, it is necessary to perform replacements based on startIndex, endIndex, and the text provided by the user. I found that without this implementation of toSpliced to string, quite a bit of additional code needs to be written to handle this logic.

and currently, JavaScript developers often rely on a combination of string methods such as `substring`, `slice`, and concatenation to achieve substring manipulation. This can lead to complex and error-prone code. The `toSpliced` method simplifies these operations, making string manipulation more straightforward and reducing the cognitive load on developers.

I've put more details in the GitHub repo. I'm looking for a TC39 member to champion this proposal.

Any feedback or questions are welcome!，thanks！

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [September 14, 2024, 12:57pm UTC](https://es.discourse.group/t/proposal-string-tospliced/2142/2 "2024-09-14T12:57:45Z")

</div>

An issue with the index based string methods is that they are the code-units based and [String.prototype.with() - #6 by michael](https://es.discourse.group/t/string-prototype-with/1756/6)

Also just a note that if this did exist I would expect it to be `toSpliced` instead of `splice` because strings are immutable.

---

<div class="post-metadata">

**Author:** ![electroluxcode](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/electroluxcode/32/2222_2.png) [@electroluxcode](https://es.discourse.group/u/electroluxcode)\
**Post date:** [September 14, 2024, 4:11pm UTC](https://es.discourse.group/t/proposal-string-tospliced/2142/3 "2024-09-14T16:11:41Z")

</div>

Thank you for your reply. changing to `tosplice` indeed better aligns with the immutability semantics of `string` . I will update my description and the README on GitHub accordingly. Thanks again.
