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
22
votes
1 answer

Checking if a string contains a particular substring in Velocity

In Velocity I have a variable called $url which contains the following string: [ContentId(2.7507), ContentId(2.7508), ContentId(1.44551)] I want to check if that string contains the substring 1.44551. This is the code I've written so far, but for…
Victoria
  • 901
  • 3
  • 15
  • 30
21
votes
1 answer

Using Substring in MySQL criteria

I'm trying to fetch all instances where the 1st letter of a person's first name is equal to P. This is what I came up with, which doesn't return anything: $sql="SELECT * FROM people WHERE SUBSTRING(FirstName,0,1) = 'P'"; Suggestions?
Ian
  • 369
  • 1
  • 2
  • 11
21
votes
3 answers

Kotlin: How do I get characters after "@" in a string?

I have a string that is an email. I want to be able to get the domain part of the email no matter what the string/email is. Essentially I'm wanting to get hold of the characters after the @ part of the string. For example, for testing@kotlin.com,…
Daniel Dramond
  • 1,538
  • 2
  • 15
  • 26
21
votes
2 answers

C# float.Parse String

I'm new in C# and need to read float values (x, y, z) from file. It looks like: 0 -0.01 -0.002 0.000833333333333 -0.01 -0.002 If Iam trying float number = float.Parse("0,54"); // it works well, but float number = float.Parse("0.54"); // gains…
TMachna
  • 279
  • 1
  • 2
  • 10
21
votes
3 answers

Powershellv2 - remove last x characters from a string

I have a file containing a few thousand lines of text. I need to extract some data from it, but the data I need is always 57 characters from the left, and 37 characters from the end. The bit I need (in the middle) is of varying length. e.g. …
IGGt
  • 2,627
  • 10
  • 42
  • 63
20
votes
8 answers

String truncate on length, but no chopping up of words allowed

I want to limit a string field length in MYSQL on a certain length, but I don't want any chopping up of words to occur. When I do: SELECT SUBSTRING('Business Analist met focus op wet- en regelgeving', 1, 28) I get this as output: Business Analist…
Ward Bekker
  • 6,316
  • 9
  • 38
  • 61
20
votes
6 answers

How to get substring with specific ranges in Swift 4?

This is using the example code from the official Swift4 doc let greeting = "Hi there! It's nice to meet you! " let endOfSentence = greeting.index(of: "!")! let firstSentence = greeting[...endOfSentence] // firstSentence == "Hi there!" But lets say…
Kent Wong
  • 566
  • 1
  • 6
  • 20
20
votes
5 answers

Python: optimal search for substring in list of strings

I have a particular problem where I want to search for many substrings in a list of many strings. The following is the gist of what I am trying to do: listStrings = [ACDE, CDDE, BPLL, ... ] listSubstrings = [ACD, BPI, KLJ, ...] The above entries…
Alopex
  • 323
  • 2
  • 8
20
votes
6 answers

How to replace a string at a particular position

Is there a way to replace a portion of a String at a given position in java script. For instance I want to replace 00 in the hours column with 12 in the below string.The substring comes at 13 to 15. Mar 16, 2010 00:00 AM
Harish
  • 3,343
  • 15
  • 54
  • 75
19
votes
9 answers

How can I get just the first ten characters of a string in C#

I have a string and I need to get just the first ten characters. Is there a way that I can do this easily. I hope someone can show me.
Hiromi Nagashima
  • 215
  • 1
  • 2
  • 3
19
votes
1 answer

How to extract a substring pattern in Postgresql

I have a column with a lot of inconsistent strings. Some of them contain a substring with a consistent pattern of '2015mmdd_AB_CD_EFG_(text)_(text)_HIJ' which I would like to extract. I feel this is a cross over case of regexp and a substring…
thenaturalist
  • 787
  • 2
  • 8
  • 15
19
votes
2 answers

Why is regex search in substring "not completely equivalent to slicing the string" in Python?

As the documentation stated, using regex.search(string, pos, endpos) is not completely equivalent to slicing the string, i.e. regex.search(string[pos:endpos]). It won't do regex matching as if the string is starting from pos, so ^ does not match the…
fikr4n
  • 3,250
  • 1
  • 25
  • 46
19
votes
7 answers

How can I check if a string has a substring from a List?

I am looking for the best way to check if a string contains a substring from a list of keywords. For example, I create a list like this: List keywords = new ArrayList<>(); keywords.add("mary"); keywords.add("lamb"); String s1 = "mary is a…
swap310
  • 768
  • 2
  • 8
  • 22
19
votes
3 answers

PowerShell Select-String from file with Regex

I am trying to get a string of text from a .sln (Visual Studio solution file) to show what project files are contained within the solution. An example line of text is Project("{xxxx-xxxxx-xxxx-xxxx-xx}") = "partofname.Genesis.Printing",…
Simon Price
  • 455
  • 2
  • 6
  • 17
19
votes
3 answers

JavaScript - Split A String

I have a variable that holds the value 'website.html'. How can I split that variable so it only gives me the 'website'? Thanks
Oliver Jones
  • 1,420
  • 7
  • 27
  • 43