What I want to do is take a string such as "this.those.that"
and get a substring to or from the nth occurrence of a character. So, from the start of the string to the 2nd occurrence of .
would return "this.those"
. Likewise, from the 2nd occurrence of .
to the end of the string would return "that"
. Sorry if my question is foggy, it's not that easy to explain. Also, please do not suggest making extra variables, and the result will be in a string and not an array.
1) Select Lookup from the drop-down list of Formula Type section; 2) Choose Find where the character appear Nth in a string in Choose a formula section; 3) Select the cell which contains the string you use, then type the specified character and nth occurrence in to the textboxes in the Arguments input section.
To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.
a. split("-", 2) will split the string upto the second occurrence of - . a.
You could do it without arrays, but it would take more code and be less readable.
Generally, you only want to use as much code to get the job done, and this also increases readability. If you find this task is becoming a performance issue (benchmark it), then you can decide to start refactoring for performance.
var str = 'this.those.that', delimiter = '.', start = 1, tokens = str.split(delimiter).slice(start), result = tokens.join(delimiter); // those.that console.log(result) // To get the substring BEFORE the nth occurence var tokens2 = str.split(delimiter).slice(0, start), result2 = tokens2.join(delimiter); // this console.log(result2)
jsFiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With