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
37
votes
2 answers

MySQL substring between two strings

I need a hand to solve a problem with my column field. I need to extract the string in between these two different "patterns" of strings for example: [...string] contract= 1234567890123350566076070666 issued= [string ...] I want to extract the…
Haohmaru
  • 568
  • 1
  • 7
  • 15
37
votes
9 answers

What is the difference between String.subString() and String.subSequence()

String.subSequence() has the following javadoc: Returns a new character sequence that is a subsequence of this sequence. An invocation of this method of the form str.subSequence(begin, end) behaves in exactly the same way as the invocation …
124697
  • 22,097
  • 68
  • 188
  • 315
36
votes
4 answers

MySQL Substring return empty value

SELECT SUBSTRING(FieldName,0,20) FROM Table I try on phpmyadmin and returned empty value. How i use this function ?
Awersione
  • 383
  • 1
  • 3
  • 6
35
votes
4 answers

Limit length of longtext field in SELECT results

I'm executing a SELECT query on a table in MySQL using the command-line interface (not a GUI client): SELECT * FROM blog_entry; One of blog_entry's fields is of type 'longtext' and is such a long piece of text that when the result is displayed in…
maxm
  • 5,161
  • 7
  • 30
  • 33
35
votes
7 answers

Get index of substring

I have char * source, and I want extract from it subsrting, that I know is beginning from symbols "abc", and ends where source ends. With strstr I can get the poiner, but not the position, and without position I don't know the length of the…
Country
  • 465
  • 3
  • 7
  • 9
35
votes
2 answers

Mysql count instances of substring, then order by

I have a problem in mySQL that goes as follows: Count the instances of a substring in a string field in a mySQL database Order the results by the number of occurrences of that substring (DESC) I have never done anything other than rudimentary…
Alan
  • 2,109
  • 5
  • 25
  • 28
35
votes
8 answers

Find substring in string but only if whole words?

What is an elegant way to look for a string within another string in Python, but only if the substring is within whole words, not part of a word? Perhaps an example will demonstrate what I mean: string1 = "ADDLESHAW GODDARD" string2 = "ADDLESHAW…
AP257
  • 89,519
  • 86
  • 202
  • 261
34
votes
6 answers

Find one string in another with case insensitive in Objective-C

My question is similar to How do I check if a string contains another string in Objective-C? How can I check if a string (NSString) contains another smaller string but with ignoring case? NSString *string = @"hello bla bla"; I was hoping for…
Jim
  • 8,874
  • 16
  • 68
  • 125
34
votes
4 answers

PHP remove characters after last occurrence of a character in a string

So the test case string may be: http://example.com/?u=ben Or http://example.com I'm trying to remove everything after the last occurrence of a '/' but only if it's not part of the 'http://'. Is this possible!? I have this so far: $url =…
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
33
votes
13 answers

Java substring: 'String index out of range'

I guess I'm getting this error because the string is trying to substring a null value. But wouldn't the ".length() > 0" part eliminate that issue? Here is the Java snippet: if (itemdescription.length() > 0) { pstmt2.setString(3,…
phill
  • 13,434
  • 38
  • 105
  • 141
33
votes
7 answers

Swift 4 'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

i've just converted my little app but i've found this error: 'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator my code is: let dateObj = dateFormatterFrom.date(from: dateStringa) if…
Raffaele Spadaro
  • 331
  • 1
  • 3
  • 3
33
votes
9 answers

C++ How to get substring after a character?

For example, if I have string x = "dog:cat"; and I want to extract everything after the ":", and return cat. What would be the way to go about doing this?
SKLAK
  • 3,825
  • 9
  • 33
  • 57
33
votes
6 answers

Why doesn't Google offer partial search? Is it because the index would be too large?

Google/GMail/etc. doesn't offer partial or prefix search (e.g. stuff*) though it could be very useful. Often I don't find a mail in GMail, because I don't remember the exact expression. I know there is stemming and such, but it's not the same,…
tom
33
votes
12 answers

How to extract numbers from a string?

I have string contains a path string="toto.titi.12.tata.2.abc.def" I want to extract only the numbers from this string. To extract the first number: tmp="${string#toto.titi.*.}" num1="${tmp%.tata*}" To extract the second…
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
32
votes
7 answers

Objective-C: How to extract part of a String (e.g. start with '#')

I have a string as shown below, NSString * aString = @"This is the #substring1 and #subString2 I want"; How can I select only the text starting with '#' (and ends with a space), in this case 'subString1' and 'subString2'? Note: Question was edited…
Zhen
  • 12,361
  • 38
  • 122
  • 199