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
484
votes
24 answers

How does String substring work in Swift

I've been updating some of my old code and answers with Swift 3 but when I got to Swift Strings and Indexing with substrings things got confusing. Specifically I was trying the following: let str = "Hello, playground" let prefixRange =…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
468
votes
5 answers

If strings are immutable in .NET, then why does Substring take O(n) time?

Given that strings are immutable in .NET, I'm wondering why they have been designed such that string.Substring() takes O(substring.Length) time, instead of O(1)? i.e. what were the tradeoffs, if any?
user541686
  • 205,094
  • 128
  • 528
  • 886
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
394
votes
20 answers

Find string between two substrings

How do I find a string between two substrings ('123STRINGabc' -> 'STRING')? My current method is like this: >>> start = 'asdf=5;' >>> end = '123jasd' >>> s = 'asdf=5;iwantthis123jasd' >>> print((s.split(start))[1].split(end)[0]) iwantthis However,…
John Howard
  • 61,037
  • 23
  • 50
  • 66
381
votes
12 answers

How to grab substring before a specified character in JavaScript?

I am trying to extract everything before the ',' comma. How do I do this in JavaScript or jQuery? I tried this and not working.. 1345 albany street, Bellevue WA 42344 I just want to grab the street address. var streetaddress= substr(addy, 0,…
Anjana Sharma
  • 4,535
  • 5
  • 37
  • 51
368
votes
15 answers

Extracting the last n characters from a string in R

How can I get the last n characters from a string in R? Is there a function like SQL's RIGHT?
Brani
  • 6,454
  • 15
  • 46
  • 49
363
votes
10 answers

How to get a string after a specific substring?

How can I get a string after a specific substring? For example, I want to get the string after "world" in my_string="hello python world, I'm a beginner" ...which in this case is: ", I'm a beginner")
havox
  • 4,537
  • 3
  • 17
  • 7
336
votes
3 answers

How do I check if a given Python string is a substring of another one?

I have two strings and I would like to check whether the first is a substring of the other. Does Python have such a built-in functionality?
snakile
  • 52,936
  • 62
  • 169
  • 241
333
votes
26 answers

Get Substring between two characters using javascript

I am trying to extract a string from within a larger string where it get everything inbetween a : and a ; Current Str = 'MyLongString:StringIWant;' Desired Output newStr = 'StringIWant'
Rob
  • 11,185
  • 10
  • 36
  • 54
316
votes
24 answers

How to check if a string contains text from an array of substrings in JavaScript?

Pretty straight forward. In javascript, I need to check if a string contains any substrings held in an array.
PercivalMcGullicuddy
  • 5,263
  • 9
  • 46
  • 65
308
votes
5 answers

How to get first 5 characters from string

How to get first 5 characters from string using php $myStr = "HelloWordl"; result should be like this $result = "Hello";
faressoft
  • 19,053
  • 44
  • 104
  • 146
307
votes
9 answers

Getting the first character of a string with $str[0]

I want to get the first letter of a string and I've noticed that $str[0] works great. I am just not sure whether this is 'good practice', as that notation is generally used with arrays. This feature doesn't seem to be very well documented so I'm…
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
289
votes
14 answers

Java - removing first character of a string

In Java, I have a String: Jamaica I would like to remove the first character of the string and then return amaica How would I do this?
Calibre2010
  • 3,729
  • 9
  • 25
  • 35
287
votes
12 answers

Remove last character from C++ string

How can I remove last character from a C++ string? I tried st = substr(st.length()-1); But it didn't work.
skazhy
  • 4,421
  • 7
  • 30
  • 32
273
votes
5 answers

Fastest way to remove first char in a String

Say we have the following string string data= "/temp string"; If we want to remove the first character / we can do by a lot of ways such as : data.Remove(0,1); data.TrimStart('/'); data.Substring(1); But, really I don't know which one has the…
Amr Badawy
  • 7,453
  • 12
  • 49
  • 84