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

How to check whether a string contains a substring in JavaScript?

Usually I would expect a String.contains() method, but there doesn't seem to be one. What is a reasonable way to check for this?
gramm
  • 18,786
  • 7
  • 27
  • 27
3587
votes
10 answers

Does Python have a string 'contains' substring method?

I'm looking for a string.contains or string.indexof method in Python. I want to do: if not somestring.contains("blah"): continue
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
3456
votes
30 answers

How to check if a string contains a substring in Bash

I have a string in Bash: string="My string" How can I test if it contains another string? if [ $string ?? 'foo' ]; then echo "It's there!" fi Where ?? is my unknown operator. Do I use echo and grep? if echo "$string" | grep 'foo'; then echo…
davidsheldon
  • 38,365
  • 4
  • 27
  • 28
2657
votes
36 answers

How do I check if a string contains a specific word?

Consider: $a = 'How are you?'; if ($a contains 'are') echo 'true'; Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?
Charles Yeung
  • 38,347
  • 30
  • 90
  • 130
2630
votes
16 answers

How do I get a substring of a string in Python?

I want to get a new string from the third character to the end of the string, e.g. myString[2:end]. If omitting the second part means 'to the end', and if you omit the first part, does it start from the start?
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
1277
votes
21 answers

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? I was hoping for something like: NSString *string = @"hello bla bla"; NSLog(@"%d",[string containsSubstring:@"hello"]); But the closest I could find was: if ([string…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
1151
votes
8 answers

What is the difference between String.slice and String.substring?

Does anyone know what the difference is between these two methods? String.prototype.slice String.prototype.substring
tmim
  • 12,091
  • 4
  • 18
  • 12
1147
votes
12 answers

How do I check if string contains substring?

I have a shopping cart that displays product options in a dropdown menu and if they select "yes", I want to make some other fields on the page visible. The problem is that the shopping cart also includes the price modifier in the text, which can be…
Jordan Garis
  • 11,489
  • 3
  • 16
  • 4
1086
votes
26 answers

Extract substring in Bash

Given a filename in the form someletters_12345_moreleters.ext, I want to extract the 5 digits and put them into a variable. So to emphasize the point, I have a filename with x number of characters then a five digit sequence surrounded by a single…
Berek Bryan
  • 13,755
  • 10
  • 32
  • 43
1011
votes
11 answers

What is the difference between substr and substring?

What is the difference between alert("abc".substr(0,2)); and alert("abc".substring(0,2)); They both seem to output “ab”.
Difference Engine
  • 12,025
  • 5
  • 21
  • 11
729
votes
16 answers

Check if a string contains a string in C++

I have a variable of type std::string. I want to check if it contains a certain std::string. How would I do that? Is there a function that returns true if the string is found, and false if it isn't?
neuromancer
  • 53,769
  • 78
  • 166
  • 223
644
votes
27 answers

How do I check if a string contains another string in Swift?

In Objective-C the code to check for a substring in an NSString is: NSString *string = @"hello Swift"; NSRange textRange =[string rangeOfString:@"Swift"]; if(textRange.location != NSNotFound) { NSLog(@"exists"); } But how do I do this in…
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
563
votes
6 answers

In Java, how do I check if a string contains a substring (ignoring case)?

I have two Strings, str1 and str2. How do I check if str2 is contained within str1, ignoring case?
trinity
  • 10,394
  • 15
  • 49
  • 67
518
votes
23 answers

How to extract the substring between two markers?

Let's say I have a string 'gfgfdAAA1234ZZZuijjk' and I want to extract just the '1234' part. I only know what will be the few characters directly before AAA, and after ZZZ the part I am interested in 1234. With sed it is possible to do something…
ria
  • 7,198
  • 6
  • 29
  • 35
501
votes
27 answers

How to trim a file extension from a String in JavaScript?

For example, assuming that x = filename.jpg, I want to get filename, where filename could be any file name (Let's assume the file name only contains [a-zA-Z0-9-_] to simplify.). I saw x.substring(0, x.indexOf('.jpg')) on DZone Snippets, but wouldn't…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
1
2 3
99 100