Find all index position in list based on partial string inside item in list
You can use enumerate inside a list-comprehension: indices = [i for i, s in enumerate(mylist) if ‘aa’ in s]
You can use enumerate inside a list-comprehension: indices = [i for i, s in enumerate(mylist) if ‘aa’ in s]
string SubString = MyString.Substring(MyString.Length-6);
The code: public class Test { public static void main(String args[]) { String string = args[0]; System.out.println(“last character: ” + string.substring(string.length() – 1)); } } The output of java Test abcdef: last character: f
The second option really isn’t the same as the others – if the string is “///foo” it will become “foo” instead of “//foo”. The first option needs a bit more work to understand than the third – I would view the Substring option as the most common and readable. (Obviously each of them as an … Read more
This would be my approach: SELECT CAST(‘<x>’ + REPLACE(data,’_’,'</x><x>’) + ‘</x>’ AS XML).value(‘/x[2]’,’int’) FROM YourTable First you transform this to an XML and then you pick the second node.. EDIT: Some more examples where this approach is usefull: CROSS APPLY: You can use this approach to get several tokens at once DECLARE @tbl TABLE(separated VARCHAR(100)); … Read more
You can use rangeOfString:options:range: and set the third argument to be beyond the range of the first occurrence. For example, you can do something like this: NSRange searchRange = NSMakeRange(0,string.length); NSRange foundRange; while (searchRange.location < string.length) { searchRange.length = string.length-searchRange.location; foundRange = [string rangeOfString:substring options:0 range:searchRange]; if (foundRange.location != NSNotFound) { // found an occurrence … Read more
You can capture the strings, then count them. It can be done by applying a list context to the capture with (): my $x = “foo”; my $y = “foo foo foo bar”; my $c = () = $y =~ /$x/g; # $c is now 3 You can also capture to an array and count … Read more
Try this test: any(substring in string for substring in substring_list) It will return True if any of the substrings in substring_list is contained in string. Note that there is a Python analogue of Marc Gravell’s answer in the linked question: from itertools import imap any(imap(string.__contains__, substring_list)) In Python 3, you can use map directly instead: … Read more
“<name> <substring>”[/.*<([^>]*)/,1] => “substring” No need to use scan, if we need only one result. No need to use Python’s match, when we have Ruby’s String[regexp,#]. See: http://ruby-doc.org/core/String.html#method-i-5B-5D Note: str[regexp, capture] → new_str or nil
At least three ways: A regular expression: var result = /[^/]*$/.exec(“foo/bar/test.html”)[0]; …which says “grab the series of characters not containing a slash” ([^/]*) at the end of the string ($). Then it grabs the matched characters from the returned match object by indexing into it ([0]); in a match object, the first entry is the … Read more