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.

412 questions
1800
votes
18 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
402
votes
24 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
  • 143,156
  • 76
  • 154
  • 173
189
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,761
  • 2
  • 21
  • 28
164
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,677
  • 2
  • 10
  • 8
146
votes
2 answers

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
  • 5,070
  • 8
  • 28
  • 27
113
votes
10 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
  • 44,854
  • 16
  • 96
  • 107
92
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
user2165
  • 1,951
  • 3
  • 20
  • 39
89
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,294
  • 12
  • 51
  • 73
64
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
  • 58,567
  • 58
  • 222
  • 373
60
votes
6 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,862
  • 22
  • 62
  • 79
59
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
  • 613
  • 1
  • 6
  • 5
58
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,955
  • 2
  • 35
  • 53
55
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
  • 555
  • 1
  • 4
  • 6
55
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
  • 359,921
  • 101
  • 625
  • 535
40
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
  • 10,139
  • 21
  • 66
  • 94
1
2 3
27 28