Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring with reverse index

How can I extract "456" from "xxx_456" where xxx is of indefinite length?

like image 776
Ricky Avatar asked Mar 08 '10 09:03

Ricky


People also ask

How do I reverse a string with index?

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).

Is substr () and substring () are same?

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 .

What is substring index in MySQL and how to use it?

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.

How do you use char index with a substring?

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.

What does the function substring_index () return?

The SUBSTRING_INDEX () function returns a substring of a string before a specified number of delimiter occurs. Required. The original string Required.

Is the substring index case sensitive?

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.


1 Answers

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

like image 185
Andy E Avatar answered Nov 07 '22 11:11

Andy E