2

I have a string 123456789.

My aim is to get 1236789 after deleting 45. I used slice method to delete the string 45.

 var x = 123456789;
 res = x.slice(4, 5); //my output is 45 

I need the output to be 1236789

Any help would be appreciated.

3 Answers 3

4

Slice before, slice after, and concatenate the resulting strings or replace the 45 with an empty string:

var x = "123456789"
var res1 = x.slice(0, 3) + x.slice(5)

console.log(res1)

var res2 = x.replace('45', '')
console.log(res2)

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

Comments

3

try replace/2.

"123456789".replace("45", "") /** 1236789 */

Or, if your input is an integer and you need and integer as an outcome,

var a = 123456789;  
parseInt(a.toString().replace("45", ""))

Comments

3

You can so this:

var x = "123456789";
var result = x.substr(0, 3) + x.substr(5);
console.log(result)

Comments

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.