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
32
votes
3 answers

Removing an item from list matching a substring

How do I remove an element from a list if it matches a substring? I have tried removing an element from a list using the pop() and enumerate method but seems like I'm missing a few contiguous items that needs to be removed: sents = ['@$\tthis…
alvas
  • 115,346
  • 109
  • 446
  • 738
31
votes
6 answers

What's the most efficient way to find one of several substrings in Python?

I have a list of possible substrings, e.g. ['cat', 'fish', 'dog']. In practice, the list contains hundreds of entries. I'm processing a string, and what I'm looking for is to find the index of the first appearance of any of these substrings. To…
Roee Adler
  • 33,434
  • 32
  • 105
  • 133
31
votes
3 answers

How to remove time-field string from a date-as-character variable?

Suppose I have a variable like this c<-c("9/21/2011 0:00:00", "9/25/2011 0:00:00", "10/2/2011 0:00:00", "9/28/2011 0:00:00", "9/27/2011 0:00:00") what's a quick way to remove all 0:00:00s so that c [1] "9/21/2011" "9/25/2011" "10/2/2011"…
David Z
  • 6,641
  • 11
  • 50
  • 101
29
votes
8 answers

php replace first occurrence of string from 0th position

I want to search and replace the first word with another in php like as follows: $str="nothing inside"; Replace 'nothing' to 'something' by search and replace without using substr output should be: 'something inside'
Ben
  • 4,392
  • 3
  • 23
  • 20
29
votes
3 answers

Why is it not a compiler error to assign to the result of a substr call?

I just discovered the most baffling error and I don't understand why the compiler did not flag it for me. If I write the following: string s = "abcdefghijkl"; cout << s << endl; s.substr(2,3) = "foo"; s.substr(8,1) = '.'; s.substr(9,1) = 4; cout…
blahedo
  • 834
  • 8
  • 19
29
votes
5 answers

Algorithm to find the most common substrings in a string

Is there any algorithm that can be used to find the most common phrases (or substrings) in a string? For example, the following string would have "hello world" as its most common two-word phrase: "hello world this is hello world. hello world repeats…
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
28
votes
7 answers

Better way to detect if a string contains multiple words

I am trying to create a program that detects if multiple words are in a string as fast as possible, and if so, executes a behavior. Preferably, I would like it to detect the order of these words too but only if this can be done fast. So far, this is…
Silver
  • 421
  • 2
  • 8
  • 17
27
votes
6 answers

How to remove a particular substring from a string?

In my C++ program, I have the string string s = "/usr/file.gz"; Here, how to make the script to check for .gz extention (whatever the file name is) and split it like "/usr/file"?
John
  • 2,035
  • 13
  • 35
  • 44
26
votes
8 answers

Unexpected behavior of Substring in C#

The definition of Substring() method in .net System.String class is like this public string Substring(int startIndex) Where startIndex is "The zero-based starting character position of a substring in this instance" as per the method definition. If…
Arghya C
  • 9,805
  • 2
  • 47
  • 66
26
votes
4 answers

Find the last substring after a character

I know many ways how to find a substring: from start index to end index, between characters etc., but I have a problem which I don't know how to solve: I have a string like for example a path: folder1/folder2/folder3/new_folder/image.jpg and the…
Ziva
  • 3,181
  • 15
  • 48
  • 80
25
votes
7 answers

How to separate letters and digits from a string in php

I have a string which is combination of letters and digits. For my application i have to separate a string with letters and digits: ex:If my string is "12jan" i hav to get "12" "jan" separately..
sandeep
  • 2,862
  • 9
  • 44
  • 54
25
votes
4 answers

How do I find the last occurrence of a substring in a Swift string?

In Objective-C I used: [@"abc def ghi abc def ghi" rangeOfString:@"c" options:NSBackwardsSearch]; But now NSBackWardsSearch seems not to exist. Could anyone provide equivalent code for Swift? I would like to be able to find the character number in…
agf119105
  • 1,770
  • 4
  • 15
  • 22
25
votes
5 answers

Getting a substring from a string after a particular word

I have below String. ABC Results for draw no 2888 I would like to extract 2888 from here. That means, I need to extract characters after no in above string. I'm always extract the number after the word no. The String contain no other no letter…
Bishan
  • 15,211
  • 52
  • 164
  • 258
24
votes
7 answers

Find substring in shell script variable

I have a string $VAR="I-UAT"; in my shell script code. I need a conditional statement to check if "UAT" is present in that string. What command should I use to get either true or false boolean as output? Or is there any other way of checking it?
batty
  • 597
  • 2
  • 6
  • 15
24
votes
2 answers

swift 3 get start index (as int) of substring

I would like to get the start and end position of a substring within a string. Example: in the string "hi this is my name"; if I provide the string "this" I would like to know the start index is 4 and end index is 7. I found several links, including…
rog
  • 363
  • 1
  • 2
  • 6