0

How can I delete a part of a string by using only the index (no regex in case the character at index exists elsewhere)?

I use this, but it seems extremely convulted!!

var str = "stringIwant to delete from";
var stringCodeLastIndex = str.length - 1;
var index2delete = 1;
var stringStart, stringEnd, finalString;

if (index2delete == 0) {
    stringStart = "";
} else {
    stringStart = str.substr(0, index2delete);
}
if (index2delete < stringCodeLastIndex) {
    stringEnd = str.substr(index2delete + 1);
} else {
    stringEnd = "";
}
finalString = stringStart + stringEnd;
2
  • you want to delete one character from the string? Commented Sep 18, 2014 at 9:44
  • it could be one or more, here one. But my code has variable lengths. Does it make a difference? Commented Sep 18, 2014 at 9:45

5 Answers 5

1

substring is smart enough to handle invalid indexes on its own:

str = "someXXXstring";
del = 4;
len = 3
str = str.substring(0, del) + str.substring(del + len);
document.body.innerText += str + ","

str = "somestringXXX";
del = 10;
len = 20
str = str.substring(0, del) + str.substring(del + len);
document.body.innerText += str + ","

str = "somestring";
del = 0;
len = 200
str = str.substring(0, del) + str.substring(del + len);
document.body.innerText += str + ","

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, much cleaner than mine! What happens if the deleted string was ALL the string? returns "" right?
@Gamemorize: yes, this is what it does.
maybe you could specify that var len is length of substring to be deleted. Anyways accepted!
1

In your case, it's easier to use slice():

finalString = str.slice(0,index2delete)+str.slice(index2delete+1)

If you want to remove more characters, you can have 2 indexes:

finalString = str.slice(0,start_index)+str.slice(endindex+1)

http://www.w3schools.com/jsref/jsref_slice_string.asp

4 Comments

Thanks! Much cleaner than mine!
You're welcome. Don't forget to approve one of the answers when you're done. Doesn't have to be mine. ;)
yeah, just trying to decide which is Cleanest!
Yours was just as clean, went with the answer with the fancy code running for ease of use for future visitors. Thanks though +1!
1

To remove one specific index from your string:

str.substr(0, indexToDelete) + str.substr(indexToDelete+1, str.length);

to remove a range of indexes from your string:

str.substr(0, startIndexToDelete) + str.substr(endIndexToDelete+1, str.length);

3 Comments

startIndexToDelete+endIndexToDelete ? Are you sure?
Yes it will remove characters between startindex and endindex
@Abdul: No, it will not, this is a wrong solution. It will remove endIndexToDelete characters after startIndexToDelete. The correct version is str.substr(0, startIndexToDelete) + str.substr(endIndexToDelete+1, str.length) @Gamemorize: yes, it should return an empty string
0
var str = "stringIwant to delete from";
var index2delete = 1;
arStr = str.split(""); // Making a JS Array of each characters
arStr.splice(index2delete,1); // Using array method to remove one entry at specified index
var finalString = arStr.join(''); // Convert Array to String

Result :

sringIwant to delete from

fiddle

2 Comments

Allow me to comment that naming an array str is a bit confusing :)
@evilpenguin, You are allowed and I edited code accordingly, thx ;)
-2

then create a substring and use replace('your substring', '') it will replace the part of your string with a empty space

1 Comment

as I said the regex (or replace) can-t be used! It would replace more than the specified index!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.