Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all characters after a certain index from a string [duplicate]

I am trying to remove all characters from a string after a specified index. I am sure there must be a simple function to do this, but I'm not sure what it is. I am basically looking for the javascript equivalent of c#'s string.Remove.

like image 647
msbg Avatar asked Mar 02 '13 02:03

msbg


2 Answers

var myStr = "asdasrasdasd$hdghdfgsdfgf";
myStr = myStr.split("$")[0];

or

var myStr = "asdasrasdasd$hdghdfgsdfgf";
myStr = myStr.substring(0, myStr.indexOf("$") - 1);
like image 131
F__M Avatar answered Sep 24 '22 04:09

F__M


You're looking for this.

string.substring(from, to)

from : Required. The index where to start the extraction. First character is at index 0
to  : Optional. The index where to stop the extraction. If omitted, it extracts the rest of the string

See here: http://www.w3schools.com/jsref/jsref_substring.asp

like image 20
Phillip Berger Avatar answered Sep 20 '22 04:09

Phillip Berger