Questions tagged [substring]

Part of a string, or the function/method that returns part of a string

substring functions/methods come in basically two flavours:

  • The parameters to the function are start index and length.
    • SQL's version uses this flavour, notably using one-based indexing
  • The parameters to the function are start index and end index. C like languages (including Java) use this flavour, and all use zero-based indexing

Definition

  • A substring is a string contained in a given string.

  • The substring can start at any index of the given string and finish in any index within the given string following the start.

  • It can be an empty string, the entire given string, or (most likely) a shorter string completely encompassed by the given string.

See Also

9558 questions
83
votes
7 answers

Python: Find a substring in a string and returning the index of the substring

I have: a function: def find_str(s, char) and a string: "Happy Birthday", I essentially want to input "py" and return 3 but I keep getting 2 to return instead. Code: def find_str(s, char): index = 0 if char in s: char…
Tyler
  • 1,933
  • 5
  • 18
  • 23
82
votes
5 answers

Regex to extract substring, returning 2 results for some reason

I need to do a lot of regex things in javascript but am having some issues with the syntax and I can't seem to find a definitive resource on this.. for some reason when I do: var tesst = "afskfsd33j" var test = tesst.match(/a(.*)j/); alert…
Rick
  • 16,612
  • 34
  • 110
  • 163
79
votes
11 answers

Extract substring from a string

what is the best way to extract a substring from a string in android?
Moe
  • 1,547
  • 3
  • 18
  • 21
77
votes
9 answers

substring index range

Code: public class Test { public static void main(String[] args) { String str = "University"; System.out.println(str.substring(4, 7)); } } Output: ers I do not really understand how the substring method works. Does the…
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
77
votes
6 answers

javascript substring

the most darndest thing! the following code prints out 'llo' instead of the expected 'wo'. i get such surprising results for a few other numbers. what am i missing here? alert('helloworld'.substring(5, 2));
akula1001
  • 4,576
  • 12
  • 43
  • 56
77
votes
4 answers

Does Python have a cleaner way to express "if x contains a|b|c|d..."?

The Pythonic way to check if a string x is a substring of y is: if x in y: Finding if x is equivalent to a, b, c, d, e, f or g is also Pythonic: if x in [a,b,c,d,e,f,g]: But checking if some string x contains either a, b, c, d, e, f or g seems…
tom
  • 2,335
  • 1
  • 16
  • 30
77
votes
2 answers

What is the idiomatic scala way of finding, if a given string contains a given substring?

I have two strings in scala and I want to find out, if the bigger string (needle) contains a smaller string (haystack). What I found is doing it with regexps and matches like this (from this…
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
73
votes
1 answer

String.Substring() seems to bottleneck this code

Introduction I have this favorite algorithm that I've made quite some time ago which I'm always writing and re-writing in new programming languages, platforms etc. as some sort of benchmark. Although my main programming language is C# I've just…
Ilhan
  • 1,309
  • 10
  • 24
69
votes
4 answers

How to return all except last 2 characters of a string?

id = '01d0'; document.write('
'+id.substr(0,-2)); How can I take a string like '01d0and get the01` (all except the last two chars)? In PHP I would use substr(0,-2) but this doesn't seem to work in JavaScript. How can I make this work?
Logan
  • 10,649
  • 13
  • 41
  • 54
68
votes
3 answers

Elasticsearch: Find substring match

I want to perform both exact word match and partial word/substring match. For example if I search for "men's shaver" then I should be able to find "men's shaver" in the result. But in case case I search for "en's shaver" then also I should be able…
68
votes
11 answers

Making sure PHP substr finishes on a word not a character

I know how to use the substr function but this will happy end a string halfway through a word. I want the string to end at the end of a word how would I go about doing this? Would it involve regular expression? Any help very much appreciated. This…
Cool Hand Luke
  • 2,120
  • 9
  • 32
  • 51
67
votes
1 answer

How to use substringToIndex in Swift?

I get compiler error at this line: UIDevice.currentDevice().identifierForVendor.UUIDString.substringToIndex(8) Type 'String.Index' does not conform to protocol 'IntegerLiteralConvertible' My intention is to get the substring, but how?
János
  • 32,867
  • 38
  • 193
  • 353
66
votes
5 answers

Cutting a string at nth occurrence of a character

What I want to do is take a string such as "this.those.that" and get a substring to or from the nth occurrence of a character. So, from the start of the string to the 2nd occurrence of . would return "this.those". Likewise, from the 2nd occurrence…
Anonymous
  • 1,823
  • 7
  • 23
  • 29
66
votes
6 answers

Substring in excel

I have a set of data that shown below on excel. R/V(208,0,32) YR/V(255,156,0) Y/V(255,217,0) R/S(184,28,16) YR/S(216,128,0) Y/S(209,171,0) R/B(255,88,80) YR/B(255,168,40) Y/B(255,216,40) And I want to separate the data in…
Celops
  • 661
  • 1
  • 5
  • 3
66
votes
4 answers

How can I use backslashes (\) in a string?

I tried many ways to get a single backslash from an executed (I don't mean an input from html). I can get special characters as tab, new line and many others then escape them to \\t or \\n or \\(someother character) but I cannot get a single…
Mik
  • 2,884
  • 2
  • 19
  • 10