JanvierDesigns
JanvierDesigns

Reputation: 81

Remove part of a string before a certain position

I have a string I want to cut and remove the first part.

Something like 'abcded-cddndcasds--XYZ--jkajsjasasasasas'

I want to remove everything before position X.

So far I can find the position of X, but can't find a quick function to remove everything before.

thanks

Upvotes: 0

Views: 1318

Answers (4)

Adil Shaikh
Adil Shaikh

Reputation: 44740

var str = 'abcded-cddndcasds--XYZ--jkajsjasasasasas';
str = "X"+str.split('X')[1];

Demo

Upvotes: 0

Deminem
Deminem

Reputation: 672

Every programming language offers standard methods in String API to achieve this thing:

a) indexOf(X) - find the first occurrence of given letter in the sentence.

b) lastIndexOf(X) - find the last occurrence of given letter in the sentence.

c) substring(start index, end index) - extracts the text based upon start and end index positions.

Example:

var str = "abcded-cddndcasds--XYZ--jkajsjasasasasas"
var index = str.indexOf("X");

if (index != -1)
   str.substring(str.indexOf("X"));

Hope this helps!

Upvotes: 0

Amit
Amit

Reputation: 15387

Try this

 $(function(){
   var str = 'abcded-cddndcasds--XYZ--jkajsjasasasasas';

   var indexOfX = str.indexOf("X");

   alert(str.substring(indexOfX,str.length));
 });

DEMO

Upvotes: 1

Adil
Adil

Reputation: 148120

You can use substring() and give the starting index and substring() will take string starting from given index to end of string.

Live Demo

res = str.substring(str.indexOf('X'));

Upvotes: 2

Related Questions