Home
Java
String Between, Before, After
This page was last reviewed on Jun 1, 2023.
Dot Net Perls
Between, before and after. Often we need to get a substring between two other substrings. The logic is simple, but a reusable method is helpful.
IndexOf and substring. These methods locate the position of strings within a source string. With additional logic, we can take substrings relative to indexes.
String indexOf
String substring
Input and output. As we begin, it is important to understand the input and output of the Java program. We have a text line that seems to define a variable between 2 parts.
Input = "DEFINE:A=TWO" Between("DEFINE:", "=TWO") = "A"
Methods. This program introduces the between, before and after methods. For the three methods, the first argument is the String we are operating upon.
Note Between returns a possible substring between the second and third arguments (a and b). On failure, it returns an empty string.
Note 2 Before() is a simple method that returns the part of the string that occurs before the first index of the substring.
Next After() does the opposite of the before() method—it is also simple and requires minimal logic.
public class Program { static String between(String value, String a, String b) { // Return a substring between the two strings. int posA = value.indexOf(a); if (posA == -1) { return ""; } int posB = value.lastIndexOf(b); if (posB == -1) { return ""; } int adjustedPosA = posA + a.length(); if (adjustedPosA >= posB) { return ""; } return value.substring(adjustedPosA, posB); } static String before(String value, String a) { // Return substring containing all characters before a string. int posA = value.indexOf(a); if (posA == -1) { return ""; } return value.substring(0, posA); } static String after(String value, String a) { // Returns a substring containing all characters after a string. int posA = value.lastIndexOf(a); if (posA == -1) { return ""; } int adjustedPosA = posA + a.length(); if (adjustedPosA >= value.length()) { return ""; } return value.substring(adjustedPosA); } public static void main(String[] args) { // Test this string. final String test = "DEFINE:A=TWO"; // Call between, before and after methods. System.out.println(between(test, "DEFINE:", "=")); System.out.println(between(test, ":", "=")); System.out.println(before(test, ":")); System.out.println(before(test, "=")); System.out.println(after(test, ":")); System.out.println(after(test, "DEFINE:")); System.out.println(after(test, "=")); } }
A A DEFINE DEFINE:A A=TWO A=TWO TWO
Tiny languages. With programs, domain-specific languages are often used for configuration files. With these methods, we can easily parse a DSL and extract parts from statements.
Detail In the main() method above, the string "DEFINE" could be used to set the variable A to the value TWO.
And The between, before and after methods can be used to parse parts of this declarative statement.
In most tasks, performance is not the primary concern. For a configuration file language, one that is rarely run, correctness is more important.
And These simple String methods help with writing correct code. The special value -1 is handled, avoiding possible exceptions.
One issue. These methods have a different behavior in a failure case. They will return an empty String, which may reduce exceptions but could cause incorrect behavior if not expected.
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.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.