Reputation: 81
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
Reputation: 44740
var str = 'abcded-cddndcasds--XYZ--jkajsjasasasasas';
str = "X"+str.split('X')[1];
Upvotes: 0
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
Reputation: 15387
Try this
$(function(){
var str = 'abcded-cddndcasds--XYZ--jkajsjasasasasas';
var indexOfX = str.indexOf("X");
alert(str.substring(indexOfX,str.length));
});
Upvotes: 1
Reputation: 148120
You can use substring() and give the starting index and substring()
will take string starting from given index to end of string.
res = str.substring(str.indexOf('X'));
Upvotes: 2