Questions tagged [startswith]

Relates to the task of finding a subset of data at the beginning of a set of data. Usually refers to strings but could be other types of data. Please accompany with a language specific tag.

335 questions
1741
votes
19 answers

How to check if a string "StartsWith" another string?

How would I write the equivalent of C#'s String.StartsWith in JavaScript? var haystack = 'hello world'; var needle = 'he'; haystack.startsWith(needle) == true Note: This is an old question, and as pointed out in the comments ECMAScript 2015 (ES6)…
sol
309
votes
23 answers

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

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…
Daryl Spitzer
  • 121,723
  • 75
  • 151
  • 166
169
votes
4 answers

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

What the best way to find if a string starts with another in Ruby (without rails)?
Guillaume Coté
  • 2,551
  • 2
  • 17
  • 28
162
votes
8 answers

Code not running in IE 11, works fine in Chrome

The following code can be run without a problem in Chrome, but throws the following error in Internet Explorer 11. Object doesn't support property or method 'startsWith' I am storing the element's ID in a variable. What is the issue? function…
Bhushan Dhamdhere
  • 1,657
  • 2
  • 8
  • 8
93
votes
8 answers

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

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…
thejh
  • 41,293
  • 15
  • 89
  • 105
88
votes
11 answers

Why does "abcd".StartsWith("") return true?

Title is the entire question. Can someone give me a reason why this happens?
Dested
  • 6,014
  • 11
  • 50
  • 71
75
votes
6 answers

Does R have function startswith or endswith like python?

> startsWith('abc', 'a') [1] TRUE > startsWith('abc', 'c') [1] FALSE > endsWith('abc', 'a') [1] FALSE > endsWith('abc', 'c') [1] TRUE
user4015990
  • 1,533
  • 3
  • 17
  • 36
55
votes
2 answers

Why is string's startswith slower than in?

Surprisingly, I find startswith is slower than in: In [10]: s="ABCD"*10 In [11]: %timeit s.startswith("XYZ") 1000000 loops, best of 3: 307 ns per loop In [12]: %timeit "XYZ" in s 10000000 loops, best of 3: 81.7 ns per loop As we all know, the in…
WKPlus
  • 6,106
  • 1
  • 32
  • 50
54
votes
7 answers

Case-insensitive string startswith in Python

Here is how I check whether mystring begins with some string: >>> mystring.lower().startswith("he") True The problem is that mystring is very long (thousands of characters), so the lower() operation takes a lot of time. QUESTION: Is there a more…
Nicolas Raoul
  • 55,003
  • 52
  • 197
  • 338
52
votes
5 answers

Regex to check with starts with http://, https:// or ftp://

I am framing a regex to check if a word starts with http:// or https:// or ftp://, my code is as follows, public static void main(String[] args) { try{ String test = "http://yahoo.com"; …
Abhishek
  • 6,554
  • 20
  • 58
  • 76
52
votes
5 answers

Why is startswith slower than slicing

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…
Andy Hayden
  • 291,328
  • 80
  • 565
  • 500
52
votes
1 answer

Go StartsWith(str string)

Is there a StartsWith(str1, str2 string) function that can check if str1 is a prefix of str2 in Go language? I want a function similar to the Java's startsWith().
Ammar
  • 3,640
  • 6
  • 25
  • 27
41
votes
1 answer

If strings starts with in PowerShell

Is there a way to check if a string starts with a string? We are checking the groupmembership from the AD user. Our AD groups look like this: S_G_share1_W The script for connecting the networkshares should only run if the groupname starts with…
JocSch
  • 425
  • 1
  • 5
  • 5
37
votes
3 answers

How do I ignore case when using startsWith and endsWith in Java?

Here's my code: public static void rightSel(Scanner scanner,char t) { /*if (!stopping)*/System.out.print(": "); if (scanner.hasNextLine()) { String orInput = scanner.nextLine; if (orInput.equalsIgnoreCase("help") { …
Matao Gearsky
  • 375
  • 1
  • 3
  • 6
32
votes
4 answers

What is the easiest way to get all strings that do not start with a character?

I am trying to parse about 20 million lines from a text file and am looking for a way to do some further manipulations on lines that do not start with question marks. I would like a solution that does not use regex matching. What I would like to do…
drbunsen
  • 8,781
  • 20
  • 62
  • 91
1
2 3
22 23