How can I extract "456" from "xxx_456" where xxx is of indefinite length?
Strings can be reversed using slicing. To reverse a string, we simply create a slice that starts with the length of the string, and ends at index 0. The slice statement means start at string length, end at position 0, move with the step -1 (or one step backward).
The difference between substring() and substr()The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .
Similar to the SUBSTRING function, the SUBSTRING INDEX function also allows having the count mentioned as negative numbers. This means, instead of traversing from left to right, the MySQL Engine will try to find a match from right to left. Understand this with the help of some examples.
Using CharIndex with Substring: First Name - substring (AuthorName,1, charindex (' ',AuthorName)) means from the first position till the first white space comes extract all the characters. Last Name - substring (AuthorName, (charindex (' ',AuthorName)) + 1, len (AuthorName)) means to extract from the first white space + 1.
The SUBSTRING_INDEX () function returns a substring of a string before a specified number of delimiter occurs. Required. The original string Required.
In this example: since we have specified the count as 1, the output would be String till the first occurrence of ‘e’ is reached. Change the count to 2 for the same character ‘e’. Now, we can see, Substring up to the second occurrence of character ‘e’ is returned. Take another example to understand that the SUBSTRING INDEX is case sensitive.
slice
works just fine in IE and other browsers, it's part of the specification and it's the most efficient method too:
alert("xxx_456".slice(-3)); //-> 456
slice Method (String) - MSDN
slice - Mozilla Developer Center
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