javascript get all characters before a certain one
// say you wanted to collect all the chars before a slash (/). myString = "12345.67890"; newString = myString.substring(0, myString.indexOf('/')); console.log(newString); // returns "12345"
Here is what the above code is Doing:
1. We create a string called myString and assign it the value “12345.67890”.
2. We create a new string called newString and assign it the value of myString.substring(0, myString.indexOf(‘/’)).
3. We log the value of newString to the console.
The substring() method takes two arguments: the starting index and the ending index.
In this case, we’re starting at index 0 and ending at the index of the first slash (/).
The indexOf() method returns the index of the first occurrence of a specified value in a string.
In this case, we’re looking for the first occurrence of a slash (/).
Since the slash is at index 5, the substring() method returns the characters from index 0 to index 5.