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

8440 questions
7419
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,623
  • 6
  • 24
  • 25
3596
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
  • 236,778
  • 296
  • 715
  • 1,125
2911
votes
26 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
  • 32,393
  • 4
  • 25
  • 27
2660
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
  • 36,649
  • 27
  • 83
  • 130
2355
votes
14 answers

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

Is there a way to substring a string in Python, to get a new string from the third character to the end of the string? Maybe like myString[2:end]? If leaving the second part means 'till the end', and if you leave the first part, does it start from…
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
1241
votes
23 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.
  • 51,850
  • 46
  • 174
  • 275
1074
votes
13 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
  • 10,759
  • 3
  • 13
  • 4
926
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
  • 9,711
  • 4
  • 16
  • 12
921
votes
10 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
  • 10,925
  • 5
  • 18
  • 11
811
votes
22 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
  • 10,925
  • 10
  • 29
  • 43
591
votes
13 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
  • 47,047
  • 74
  • 161
  • 217
565
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
  • 29,619
  • 13
  • 57
  • 72
542
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
  • 9,806
  • 10
  • 46
  • 65
457
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
  • 189,354
  • 112
  • 476
  • 821
410
votes
19 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
  • 364,799
  • 232
  • 1,155
  • 1,198
1
2 3
99 100