Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is StringUtils.substringsBetween() in Java?

Abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The substringsBetween() method

substringsBetween() is a staticIn Java, static methods can be called without creating a class object. method in the StringUtils library. For a given string, substringsBetween() returns the set of substrings between a pair of open and close strings in the form of an array.

Try substringBetween to return the first matched substring between the open and close strings.

How to import StringUtils

The definition of StringUtils is available in the Apache Commons Lang package, which we can add to the Maven project by including the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

Import the StringUtils using the line below:


import org.apache.commons.lang3.StringUtils;

Syntax

The below code snippet demonstrates how we use the substringsBetween() function.


public static String[] substringsBetween(final String str, final String open, final String close)

Parameters

The substringsBetween() method requires three input arguments:

  • str: the String for which substrings will be returned.
  • open: the String that will appear before the returned substring(s) in str.
  • close: the String that will appear after the returned substring(s) in str.

Return value

substringsBetween() returns an array containing all substrings found in str between open and close.

Example

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Explanation

The maven dependency for StringUtils is included in the pom.xml file.

The Main.java is explained below:

  • Line 1: We import the StringUtils class.
  • Line 6: We declare and initialize the String text.
  • Line 7: We declare and initialize the String open.
  • Line 8: We declare and initialize the String close.
  • Line 9: We use substringsBetween() to find and return all substrings found in text between open and close and print the result. The output shows three substrings that match found between curly braces.

RELATED TAGS

java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring