0

How can i remove from a string between two indexOf

i have this jsFiddle

here is my code :

 var x = "M 178.6491699876038 23.419570090792845 C 183.6491699876038 
23.419570090792845 186.47776067057902 30.902823043098138 190.3670728596699 
     41.19229585251793 L 194.25638504876076 51.48176866193772" ; 
  var c = x.indexOf('C') ; 
  var L = x.indexOf('L') ;
  var final = x.slice (c,L) ;
  console.log(final) ;

this code will result in returning the removed part of the string

QUESTION how can i return the original string after removing the part between C and L

3
  • 1
    whats wrong with just using X? Commented Jul 19, 2012 at 17:52
  • what do you mean by using x ?????????????????????????????? Commented Jul 20, 2012 at 0:49
  • 1
    Nevermind, from your wording, I thought you just wanted your oringinal string, which would of been in X, not your orginal string minus the middle. Commented Jul 20, 2012 at 12:06

5 Answers 5

9
var c = x.indexOf('C') ; 
var L = x.indexOf('L') ;

var remaining = x.slice(0, c) + x.slice(L);

Fiddle:

http://jsfiddle.net/ePbCP/

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

3 Comments

var remaining = x.slice(0, c-1) + x.slice(L-1, x.length); result: M 178.6491699876038 23.419570090792845 L 194.25638504876076 51.4817686619377
@Jan-StefanJanetzky: That removes the 'L'. I don't think that is the desired result.
@Jan-StefanJanetzky: Why is that better than what's posted in the answer?
4

Just replace that part of the original string with nothing :

var x = "M 178.6491699876038 23.419570090792845 C 183.6491699876038 23.419570090792845 186.47776067057902 30.902823043098138 190.3670728596699 41.19229585251793 L 194.25638504876076 51.48176866193772" ; 
var c = x.indexOf('C') ; 
var L = x.indexOf('L') ;

var final = x.slice (c,L) ;
console.log(x.replace(final, ''));

FIDDLE

Comments

2

You can use 2 substrings for this.

var c = x.indexOf('C') ; 
var L = x.indexOf('L') ;

var y = x.substring(0, c) + x.substring(L);

Comments

1

Try:

var x = "M 178.6491699876038 23.419570090792845 C 183.6491699876038 23.419570090792845 186.47776067057902 30.902823043098138 190.3670728596699 41.19229585251793 L 194.25638504876076 51.48176866193772";
var c = x.indexOf('C');
var L = x.indexOf('L');
var final = x.substring(0, c) + x.substring(L, x.length);
console.log(final);

Comments

1

You can replace the final string with empty string to get the remaining part.

var remaining = x.replace(final,'');

Live Demo

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.