最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Remove part of string between two indexOf in javascript - Stack Overflow

programmeradmin5浏览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

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

Share Improve this question asked Jul 19, 2012 at 17:48 Mina GabrielMina Gabriel 25.1k26 gold badges99 silver badges128 bronze badges 3
  • 1 whats wrong with just using X? – Limey Commented Jul 19, 2012 at 17:52
  • what do you mean by using x ?????????????????????????????? – Mina Gabriel 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. – Limey Commented Jul 20, 2012 at 12:06
Add a ment  | 

5 Answers 5

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

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

Fiddle:

http://jsfiddle/ePbCP/

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

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);

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);

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

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

Live Demo

发布评论

评论列表(0)

  1. 暂无评论