All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.string.Substring Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.string;

import java.util.Objects;

public class Substring {

	/**
	 * Uses the given prefix and suffix to extract a substring from the given
	 * text.
	 * 

* For the prefix, the first match is found. For the suffix, the last match * is found. Hence, this method extracts the longest possible substring from * the given text. *

* If the given text is empty, or if there is no match for either the prefix * or the suffix, or if there is a suffix match before a prefix match, an * empty {@link String} is returned. *

* An empty prefix or suffix matches the first or last index in the given * text, respectively. If both affixes are empty, the given text is * returned. * * @param text * the {@link String} to extract a substring from (never * null) * @param prefix * the prefix before the substring to extract (never null) * @param suffix * the suffix after the substring to extract (never null) * @return the enclosed {@link String} (never null) * @throws NullPointerException * if either of the given parameters is null */ public static String between(String text, String prefix, String suffix) { Objects.requireNonNull(text); Objects.requireNonNull(prefix); Objects.requireNonNull(suffix); int prefixIndex = text.indexOf(prefix); int suffixIndex = text.lastIndexOf(suffix); if (prefixIndex >= 0 && suffixIndex >= 0) { int begin = prefixIndex + prefix.length(); int end = suffixIndex; if (begin < end) { return text.substring(begin, end); } } return ""; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy