2

I don`t know the easy way how to remove text from text by range in JS.

I read a lot of questions there, but no one help me.

I dont want to user replace, etc. Just remove.

Example:

jakxxxjakxxx => remove 4 - 6 returns jakjakxxx

jakxxxjakxxx => remove 0 - 2 returns xxxjakxxx

Is there any function is standard js library?

hiacliok
  • 239
  • 3
  • 7
  • 1
    Removing _is_ replacing. Strings are immutable, so to remove characters you derive a new string with the characters that you want removed from the original string replaced with empty strings. Do you want to remove based on indices, or do you "know" what you want to replace (in which case the .replace() function will work just fine for your needs). – Mike 'Pomax' Kamermans Aug 27 '18 at 17:24
  • 1
    *I dont want to user replace, etc. Just remove.* So, if I replace your car with nothing, have I not removed your car? Also, where is the code you've tried? – Scott Marcus Aug 27 '18 at 17:24
  • You are describing [str.substring](https://www.w3schools.com/jsref/jsref_substring.asp)... – AmmoPT Aug 27 '18 at 17:25
  • So, then what result will be when I call replace "xxx" by "" - empty string – hiacliok Aug 27 '18 at 17:25
  • @AmmoPT No he's not. – Scott Marcus Aug 27 '18 at 17:25
  • @AmmoPT they are not. They are describing `str.replace(str.substring(...), '')` and asking if there's a single function that does that. – Mike 'Pomax' Kamermans Aug 27 '18 at 17:25
  • @hiacliok It will remove "xxx" and not replace it with anything -- in other words, remove. – Scott Marcus Aug 27 '18 at 17:26
  • replace will replace on wrong index – hiacliok Aug 27 '18 at 17:27
  • 2
    @hiacliok .replace() replaces exactly where you tell it to, so if you think it's doing it wrong, show the code you tried and explain why you think that's wrong (and remember that JS starts counting positions at 0, like a lot of other languages. If you start at '3', that's the fourth index, and every JS programmer expects that. Making '4' the fourth index would be extremely bad practice). It sounds like what you're most likely looking for can be done with `replace()` and passing a *replacement function*, which is the lesser-known way to invoke .replace() in Javascript. – Mike 'Pomax' Kamermans Aug 27 '18 at 17:27
  • It depends on what you supply as the value to replace, `"jakxxxj".replace("xxx","")` would remove the first "xxx" and `"xjakxxx".replace("xxx","")` would remove the second. – Scott Marcus Aug 27 '18 at 17:28
  • replace string with empty ```string.replace("xxx", "")``` – dorintufar Aug 27 '18 at 17:28
  • maybe this is a bit confusing, your number are one based, whereas arrays and string are zero based. the result is moved by one. – Nina Scholz Aug 27 '18 at 17:38
  • It looks like @hiacliok simply made some mistakes in writing the indices in this question. If you use (3,6) and (0,3) then these match javascript's way of indexing substrings. If they need "not the javascript way of counting indices" then the first thing would really need to be to stop using that anti-pattern way of counting indices and work within JS, not flight against it. – Mike 'Pomax' Kamermans Aug 27 '18 at 17:45

3 Answers3

4

I'd offer a different solution than Nina's, here it is with 2 substring calls:

function remove(string, from, to) {
  return string.substring(0, from) + string.substring(to);
}

No need to transform into an array iterating the characters.

Note: Remember that we start counting from 0.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
3

You could filter the letters.

function remove(string, from, to) {
    return [...string].filter((_, i) => i < from || i > to).join('');
}

console.log(remove('jakxxxjakxxx', 4, 6)); // jakjakxxx 
console.log(remove('jakxxxjakxxx', 0, 2)); // xxxjakxxx
                    012345678901

Or slice the string.

function remove(string, from, to) {
    return string.slice(0, from) + string.slice(to);
}

console.log(remove('jakxxxjakxxx', 4, 6)); // jakjakxxx 
console.log(remove('jakxxxjakxxx', 0, 2)); // xxxjakxxx
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Based on your follow-up in the comments: use .substring() and .replace(), remembering that Javascript (like almost every other language) uses offsets starting at 0, not 1, and reading the substring function documentation to make sure that its arguments are provided as "start position, inclusive, end position, exclusive":

var a = "jakxxxjakxxx";
var b = "jakxxxjakxxx";

var c = a.replace(a.substring(3,6)); // => "jakjakxxx"
var d = b.replace(a.substring(0,3)); // => "xxxjakxxx"

Done. House that in its own function with argument rewriting if you use it a lot, but to achieve what you want to achieve, you just need two built-ins, but the one thing you should not be doing is trying to make this work with your own personal choice of which numbers indicate which index in a string or a slice: work with the JS conventions, don't bake anti-patterns into your source code.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153