Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LANG-1687] Propose update from substringBetween doc and new method #1038

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8778,17 +8778,59 @@ public static String substringBetween(final String str, final String tag) {
*
* @param str the String containing the substring, may be null
* @param open the String before the substring, may be null
* @param close the String after the substring, may be null
* @param close the String after the substring, may be null - search first match from left to right, after the <code>open</code> String found
* @return the substring, {@code null} if no match
* @since 2.0
*/
public static String substringBetween(final String str, final String open, final String close) {
return substringBetween(str, open, close, false);
}
/**
* Gets the String that is nested in between two Strings.
* Only the first match is returned.
*
* <p>A {@code null} input String returns {@code null}.
* A {@code null} open/close returns {@code null} (no match).
* An empty ("") open and close returns an empty string.</p>
*
* <pre>
* StringUtils.substringBetween("wx[b]yz", "[", "]", *) = "b"
* StringUtils.substringBetween(null, *, *, *) = null
* StringUtils.substringBetween(*, null, *, *) = null
* StringUtils.substringBetween(*, *, null, *) = null
* StringUtils.substringBetween("", "", "", *) = ""
* StringUtils.substringBetween("", "", "]", *) = null
* StringUtils.substringBetween("", "[", "]", *) = null
* StringUtils.substringBetween("yabcz", "", "", false) = ""
* StringUtils.substringBetween("yabcz", "", "", true) = "yabcz"
* StringUtils.substringBetween("yabcz", "y", "z", *) = "abc"
* StringUtils.substringBetween("yabczyabcz", "y", "z", false) = "abc"
* StringUtils.substringBetween("yabczyabcz", "y", "z", true) = "abczyabc"
* StringUtils.substringBetween("zabcyabc", "y", "z", *) = null
* </pre>
*
* @param str the String containing the substring, may be null
* @param open the String before the substring, may be null
* @param close the String after the substring, may be null
* @param lastClose set to <code>true</code> if close String must be searched from the end of the String <code>str</code>
* @return the substring, {@code null} if no match
* @since 3.13
*/
public static String substringBetween(final String str, final String open, final String close, final boolean lastClose) {
if (!ObjectUtils.allNotNull(str, open, close)) {
return null;
}
final int start = str.indexOf(open);
if (start != INDEX_NOT_FOUND) {
final int end = str.indexOf(close, start + open.length());
int end = INDEX_NOT_FOUND;
if (!lastClose) {
end = str.indexOf(close, start + open.length());
} else {
end = str.lastIndexOf(close);
if (end < start + open.length()) {
end = INDEX_NOT_FOUND;
}
}
if (end != INDEX_NOT_FOUND) {
return str.substring(start + open.length(), end);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,38 @@ public void testSubstringBetween_StringStringString() {
assertEquals("abc", StringUtils.substringBetween("yabczyabcz", "y", "z"));
}

@Test
public void testSubstringBetween_StringStringStringBoolean() {
// same tests as testSubstringBetween_StringStringString with true parameter
assertNull(StringUtils.substringBetween(null, "", "", true));
assertNull(StringUtils.substringBetween("", null, "", true));
assertNull(StringUtils.substringBetween("", "", null, true));
assertEquals("", StringUtils.substringBetween("", "", "", true));

// not same behaviour for boolean param
assertEquals("", StringUtils.substringBetween("foo", "", "", false));
assertEquals("foo", StringUtils.substringBetween("foo", "", "", true));

assertNull(StringUtils.substringBetween("foo", "", "]", true));
assertNull(StringUtils.substringBetween("foo", "[", "]", true));

assertEquals(" ", StringUtils.substringBetween(" ", " ", " ", true));
assertEquals("bar", StringUtils.substringBetween("<foo>bar</foo>", "<foo>", "</foo>", true) );

// testing javadoc examples
assertEquals("b", StringUtils.substringBetween("wx[b]yz", "[", "]", false) );
assertEquals("b", StringUtils.substringBetween("wx[b]yz", "[", "]", true) );

assertEquals("abc", StringUtils.substringBetween("yabcz", "y", "z", false) );
assertEquals("abc", StringUtils.substringBetween("yabcz", "y", "z", true) );

assertEquals("abc", StringUtils.substringBetween("yabczyabcz", "y", "z", false));
assertEquals("abczyabc", StringUtils.substringBetween("yabczyabcz", "y", "z", true));

assertNull(StringUtils.substringBetween("zabcyabc", "y", "z", false));
assertNull(StringUtils.substringBetween("zabcyabc", "y", "z", true));
}

/**
* Tests the substringsBetween method that returns a String Array of substrings.
*/
Expand Down