String Between, Before and After Functions
This page was last reviewed on Mar 22, 2023.
Dot Net Perls
Between, before, after. A string contains the syntax of a simple language. It has characters between, before and after other strings.
With special functions, we can extract these substrings. Functions like Between, Before and After get substrings based on the surrounding text. This helps parse a simple language.
Input and output. When parsing strings, we may need to extract a variable name in some way. Using Between() should yield the output shown here.
Input = "DEFINE:A=TWO" Between("DEFINE:", "=TWO") = "A"
An example program. This module has implementations for the Between, Before and After string functions. These functions take substrings based on adjacent substrings.
Note Between uses IndexOf and LastIndexOf to locate the positions of the two specified strings. It returns the substring in between.
String IndexOf
String LastIndexOf
Note 2 Before calls IndexOf to find the argument string. Then it returns the characters before that string's location.
Next After finds the last occurrence of the argument string with LastIndexOf. It then returns the substring before that point.
Module Module1 Function Between(value As String, a As String, b As String) As String ' Get positions for both string arguments. Dim posA As Integer = value.IndexOf(a) Dim posB As Integer = value.LastIndexOf(b) If posA = -1 Then Return "" End If If posB = -1 Then Return "" End If Dim adjustedPosA As Integer = posA + a.Length If adjustedPosA >= posB Then Return "" End If ' Get the substring between the two positions. Return value.Substring(adjustedPosA, posB - adjustedPosA) End Function Function Before(value As String, a As String) As String ' Get index of argument and return substring up to that point. Dim posA As Integer = value.IndexOf(a) If posA = -1 Then Return "" End If Return value.Substring(0, posA) End Function Function After(value As String, a As String) As String ' Get index of argument and return substring after its position. Dim posA As Integer = value.LastIndexOf(a) If posA = -1 Then Return "" End If Dim adjustedPosA As Integer = posA + a.Length If adjustedPosA >= value.Length Then Return "" End If Return value.Substring(adjustedPosA) End Function Sub Main() Dim test As String = "DEFINE:A=TWO" ' Test the Between Function. Console.WriteLine(Between(test, "DEFINE:", "=")) Console.WriteLine(Between(test, ":", "=")) ' Test the Before Function. Console.WriteLine(Before(test, ":")) Console.WriteLine(Before(test, "=")) ' Test the After Function. Console.WriteLine(After(test, ":")) Console.WriteLine(After(test, "DEFINE:")) Console.WriteLine(After(test, "=")) End Sub End Module
In Main, we parse a simple example string. We use a simple language syntax that could be used to define variables. We parse this string with Between, Before and After.
Tip A Dictionary could be used to store the key and value pairs returned by the parsing method.
For good performance, avoiding substrings and IndexOf calls is beneficial. If in your code the above functions cause extra work, adding more custom methods is worth consideration.
A review. Sometimes it is helpful to create functions that extract parts of strings. Substring() could be used directly. But this could become complicated and prone to errors.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 22, 2023 (edit).
© 2007-2024 Sam Allen.