startswith

How do I find if a string starts with an other string in Ruby?

感情迁移 提交于 2019-11-28 18:04:04
What the best way to find if a string starts with another in Ruby (without rails)? steenslag puts 'abcdefg'.start_with?('abc') #=> true [edit] This is something I didn't know before this question: start_with takes multiple arguments. 'abcdefg'.start_with?( 'xyz', 'opq', 'ab') Since there are several methods presented here, I wanted to figure out which one was fastest. Using Ruby 1.9.3p362: irb(main):001:0> require 'benchmark' => true irb(main):002:0> Benchmark.realtime { 1.upto(10000000) { "foobar"[/\Afoo/] }} => 12.477248 irb(main):003:0> Benchmark.realtime { 1.upto(10000000) { "foobar" =~ /

Angular JS 'Startswith' custom filter

混江龙づ霸主 提交于 2019-11-28 10:54:47
So I've been trying a while to make a custom filter that searches for the 'Startswith' parameters rather than the 'Contains'. Every filter that I've written haven't seem to work properly. Here is an example of what I'm trying to achieve ---> http://jsfiddle.net/DMSChris/9ptr9/ function FilterCtrl() { var scope = this; scope.doFilter = function(elem) { if(!scope.searchText) return true; return elem.last_name.toLowerCase().indexOf( scope.searchText.toLowerCase()) == 0; }; } http://jsbin.com/OyubElO/1/edit - Here is where I'm at right now. <ul border="1px" ng-repeat="msg in messages | filter

How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?

穿精又带淫゛_ 提交于 2019-11-27 17:02:08
How do I implement the following (Python pseudocode) in C++? if argv[1].startswith('--foo='): foo_value = int(argv[1][len('--foo='):]) (For example, if argv[1] is --foo=98 , then foo_value is 98 .) Update: I'm hesitant to look into Boost, since I'm just looking at making a very small change to a simple little command-line tool (I'd rather not have to learn how to link in and use Boost for a minor change). If you're already using Boost, you can do it with boost string algorithms + boost lexical cast: #include <boost/algorithm/string/predicate.hpp> #include <boost/lexical_cast.hpp> try { if

Why is startswith slower than slicing

落花浮王杯 提交于 2019-11-27 14:33:54
Why is the implementation of startwith slower than slicing? In [1]: x = 'foobar' In [2]: y = 'foo' In [3]: %timeit x.startswith(y) 1000000 loops, best of 3: 321 ns per loop In [4]: %timeit x[:3] == y 10000000 loops, best of 3: 164 ns per loop Surprisingly, even including calculation for the length, slicing still appears significantly faster: In [5]: %timeit x[:len(y)] == y 1000000 loops, best of 3: 251 ns per loop Note: the first part of this behaviour is noted in Python for Data Analysis (Chapter 3), but no explanation for it is offered. . If helpful: here is the C code for startswith ; and

How do I find if a string starts with another string in Ruby?

a 夏天 提交于 2019-11-27 11:23:54
问题 What the best way to find if a string starts with another in Ruby (without rails)? 回答1: puts 'abcdefg'.start_with?('abc') #=> true [edit] This is something I didn't know before this question: start_with takes multiple arguments. 'abcdefg'.start_with?( 'xyz', 'opq', 'ab') 回答2: Since there are several methods presented here, I wanted to figure out which one was fastest. Using Ruby 1.9.3p362: irb(main):001:0> require 'benchmark' => true irb(main):002:0> Benchmark.realtime { 1.upto(10000000) {

Does R have function startswith or endswith like python? [closed]

守給你的承諾、 提交于 2019-11-27 04:43:59
The question is very clear in the title. As added to base in 3.3.0 , startsWith (and endsWith ) are exactly this. > startsWith("what", "wha") [1] TRUE > startsWith("what", "ha") [1] FALSE https://stat.ethz.ch/R-manual/R-devel/library/base/html/startsWith.html Not inbuilt like that. Options include grepl and substr . x <- 'ABCDE' grepl('^AB', x) # starts with AB? grepl('DE$', x) # ends with DE? substr(x, 1, 2) == 'AB' substr('ABCDE', nchar(x)-1, nchar(x)) == 'DE' The dplyr package's select statement supports starts_with and ends_with . For example, this selects the columns of the iris data

How to check if a string starts with another string in C?

不羁岁月 提交于 2019-11-27 04:27:17
Is there something like startsWith(str_a, str_b) in the standard C library? It should take pointers to two strings that end with nullbytes, and tell me whether the first one also appears completely at the beginning of the second one. Examples: "abc", "abcdef" -> true "abcdef", "abc" -> false "abd", "abdcef" -> true "abc", "abc" -> true T.J. Crowder Apparently there's no standard C function for this. So: bool startsWith(const char *pre, const char *str) { size_t lenpre = strlen(pre), lenstr = strlen(str); return lenstr < lenpre ? false : memcmp(pre, str, lenpre) == 0; } Note that the above is

Angular JS 'Startswith' custom filter

我的未来我决定 提交于 2019-11-27 03:53:14
问题 So I've been trying a while to make a custom filter that searches for the 'Startswith' parameters rather than the 'Contains'. Every filter that I've written haven't seem to work properly. Here is an example of what I'm trying to achieve ---> http://jsfiddle.net/DMSChris/9ptr9/ function FilterCtrl() { var scope = this; scope.doFilter = function(elem) { if(!scope.searchText) return true; return elem.last_name.toLowerCase().indexOf( scope.searchText.toLowerCase()) == 0; }; } http://jsbin.com

jQuery - How to select value by attribute name starts with

亡梦爱人 提交于 2019-11-26 22:57:57
I want to select attribute value by giving attribute name (only starts with) For instance if we have html tag <div class = "slide" data-confirmID = "46" confirmID = "54"/> I want to select the value from the attribute starts with data- Thanks in advance for the help. If you want all data-* attributes, you can iterate through jq data object: $('.slide').each(function(){ for(data in $(this).data()) console.log(data); // returns confirmID so element as an attribute `data-confirmID` }); But this data object can contains other keys which aren't attribute, setted for example by some plugins. EDIT To

How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?

核能气质少年 提交于 2019-11-26 18:49:15
问题 How do I implement the following (Python pseudocode) in C++? if argv[1].startswith('--foo='): foo_value = int(argv[1][len('--foo='):]) (For example, if argv[1] is --foo=98 , then foo_value is 98 .) Update: I'm hesitant to look into Boost, since I'm just looking at making a very small change to a simple little command-line tool (I'd rather not have to learn how to link in and use Boost for a minor change). 回答1: If you're already using Boost, you can do it with boost string algorithms + boost