Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
stringutils

What is StringUtils.reverseDelimited() 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.

Overview

reverseDelimited() is a staticthe methods in Java that can be called without creating an object of the class. method of the StringUtils class that is used to reverse a string that is delimited by a specific character.

How to import StringUtils

The definition of StringUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding 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.

You can import the StringUtils class as follows:


import org.apache.commons.lang3.StringUtils;

Syntax


public static String reverseDelimited(final String str, final char separatorChar)

Parameters

  • final String str: The string to reverse.
  • final char separatorChar: The delimiter character to use.

Return value

This method returns the reversed string.

Code

import org.apache.commons.lang3.StringUtils;
public class Main{
public static void main(String[] args) {
// Example 1
String string = "hello;educative;edpresso";
char sep = ';';
System.out.printf("StringUtils.reverseDelimited('%s', %s) = '%s'", string, sep, StringUtils.reverseDelimited(string, sep));
System.out.println();
// Example 2
string = "hello-educative-edpresso";
sep = '-';
System.out.printf("StringUtils.reverseDelimited('%s', %s) = '%s'", string, sep, StringUtils.reverseDelimited(string, sep));
System.out.println();
}
}

Example 1

  • string - "hello;educative;edpresso"
  • separator - ';'

The method returns edpresso;educative;hello, i.e., reversing the characters separated by the delimiter.

Example 2

  • string - "hello-educative-edpresso"
  • separator - '-'

The method returns edpresso-educative-hello, i.e., reversing the characters separated by the delimiter.

Output

The output of the code will be as follows:


StringUtils.reverseDelimited('hello;educative;edpresso', ;) = 'edpresso;educative;hello'
StringUtils.reverseDelimited('hello-educative-edpresso', -) = 'edpresso-educative-hello'

RELATED TAGS

java
communitycreator
stringutils

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