startswith

Determine if string starts with letters A through I

﹥>﹥吖頭↗ 提交于 2019-11-30 23:40:42
问题 I've got a simple java assignment. I need to determine if a string starts with the letter A through I. I know i have to use string.startsWith(); but I don't want to write, if(string.startsWith("a")); all the way to I, it seems in efficient. Should I be using a loop of some sort? 回答1: You don't need regular expressions for this. Try this, assuming you want uppercase only: char c = string.charAt(0); if (c >= 'A' && c <= 'I') { ... } If you do want a regex solution however, you can use this

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

送分小仙女□ 提交于 2019-11-30 17:00:01
This question already has an answer here: startsWith() method of string ignoring case 7 answers 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") { System.out.println("The following commands are available:"); System.out.println(" 'help' : displays this menu"); System.out.println(" 'stop' : stops the program"); System.out.println(" 'topleft' : makes right triangle alligned left and to the top"); System.out.println(" 'topright' : makes

Why do Strings start with a “” in Java? [duplicate]

偶尔善良 提交于 2019-11-30 16:27:52
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Why does “abcd”.StartsWith(“”) return true? Whilst debugging through some code I found a particular piece of my validation was using the .startsWith() method on the String class to check if a String started with a blank character Considering the following : public static void main(String args[]) { String s = "Hello"; if (s.startsWith("")) { System.out.println("It does"); } } It prints out It does My question is,

Use Python to remove lines in a files that start with an octothorpe?

此生再无相见时 提交于 2019-11-30 06:46:50
This seems like a straight-forward question but I can't seem to pinpoint my problem. I am trying to delete all lines in a file that start with an octothorpe (#) except the first line. Here is the loop I am working with: for i, line in enumerate(input_file): if i > 1: if not line.startswith('#'): output.write(line) The above code doesn't seem to work. Does anyone known what my problem is? Thanks! You aren't writing out the first line: for i, line in enumerate(input_file): if i == 0: output.write(line) else: if not line.startswith('#'): output.write(line) Keep in mind also that enumerate (like

Why is string's startswith slower than in?

廉价感情. 提交于 2019-11-30 04:10:13
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 operation needs to search the whole string and startswith just needs to check the first few characters, so startswith should be more efficient. When s is big enough, startswith is faster: In [13]: s="ABCD"*200 In [14]: %timeit s.startswith("XYZ") 1000000 loops, best of 3: 306 ns per loop In [15]: %timeit "XYZ" in s 1000000 loops, best of 3: 666 ns per

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

家住魔仙堡 提交于 2019-11-30 02:40:07
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"; System.out.println(test.matches("^(http|https|ftp)://")); } finally{ } } It prints false . I also checked stackoverflow post Regex to test if string begins with http:// or https:// The regex seems to be right but why is it not matching?. I even tried ^(http|https|ftp)\:// and ^(http|https|ftp)\\:// Prince John Wesley You need a whole input match here. System.out.println(test.matches("^(http|https|ftp)://.*$"))

Why does string.StartsWith(“\\u2D2D”) always return true?

孤街浪徒 提交于 2019-11-30 02:03:08
I was fiddling around with parsing in C# and found that for every string I tried, string.StartsWith("\u2D2D") will return true. Why is that? It seems it works with every char. Tried this code with .Net 4.5 the Debugger did not break. for (char i = char.MinValue; i < char.MaxValue; i++) { if(!i.ToString().StartsWith("\u2d2d")) { Debugger.Break(); } } I think I'll have a try. From what I get, is that U+2D2D was added in Unicode v6.1 ( source / source ). The .NET framework, or the native calls rather, support a lower version: The culture-sensitive sorting and casing rules used in string

Use Python to remove lines in a files that start with an octothorpe?

ε祈祈猫儿з 提交于 2019-11-29 06:54:07
问题 This seems like a straight-forward question but I can't seem to pinpoint my problem. I am trying to delete all lines in a file that start with an octothorpe (#) except the first line. Here is the loop I am working with: for i, line in enumerate(input_file): if i > 1: if not line.startswith('#'): output.write(line) The above code doesn't seem to work. Does anyone known what my problem is? Thanks! 回答1: You aren't writing out the first line: for i, line in enumerate(input_file): if i == 0:

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

南笙酒味 提交于 2019-11-29 00:16:51
问题 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"; System.out.println(test.matches("^(http|https|ftp)://")); } finally{ } } It prints false . I also checked stackoverflow post Regex to test if string begins with http:// or https:// The regex seems to be right but why is it not matching?. I even tried ^(http|https|ftp)\:// and ^(http|https|ftp)\\:// 回答1: You

Why is string's startswith slower than in?

走远了吗. 提交于 2019-11-28 23:32:39
问题 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 operation needs to search the whole string and startswith just needs to check the first few characters, so startswith should be more efficient. When s is big enough, startswith is faster: In [13]: s="ABCD"*200 In [14]: %timeit s.startswith("XYZ")