Created
December 20, 2018 15:11
-
-
Save gaalha/c1fcfa64b925b350dcf89af2ccb0ab65 to your computer and use it in GitHub Desktop.
String utils fro Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils; | |
/** | |
* Edited by Edgar Mejía on 08/11/18. | |
* @author https://www.dotnetperls.com/between-before-after-java | |
* @author http://www.java2s.com/Code/Java/Data-Type/String-substring.htm | |
*/ | |
public class StrUtils { | |
private StrUtils() { | |
} | |
/** | |
* Checks if a String is empty ("") or null. | |
* | |
* StrUtils.isEmpty(null) = true | |
* StrUtils.isEmpty("") = true | |
* StrUtils.isEmpty(" ") = false | |
* StrUtils.isEmpty("bob") = false | |
* StrUtils.isEmpty(" bob ") = false | |
* | |
* NOTE: This method changed in Lang version 2.0. | |
* It no longer trims the String. | |
* That functionality is available in isBlank(). | |
* | |
* @param str the String to check, may be null | |
* @return true if the String is empty or null | |
*/ | |
public static boolean isEmpty(String str) { | |
return str == null || str.length() == 0; | |
} | |
/** | |
* Gets the substring before the first occurrence of a separator. | |
* The separator is not returned. | |
* | |
* A null string input will return null. | |
* An empty ("") string input will return the empty string. | |
* A null separator will return the input string. | |
* | |
* | |
* StrUtils.before(null, *) = null | |
* StrUtils.before("", *) = "" | |
* StrUtils.before("abc", "a") = "" | |
* StrUtils.before("abcba", "b") = "a" | |
* StrUtils.before("abc", "c") = "ab" | |
* StrUtils.before("abc", "d") = "abc" | |
* StrUtils.before("abc", "") = "" | |
* StrUtils.before("abc", null) = "abc" | |
* | |
* @param str the String to get a substring from, may be null | |
* @param separator the String to search for, may be null | |
* @return the substring before the first occurrence of the separator, | |
* null if null String input | |
* @since 2.0 | |
*/ | |
public static String before(String str, String separator) { | |
// Return substring containing all characters before a string. | |
int posA = str.indexOf(separator); | |
if (posA == -1) { | |
return ""; | |
} | |
return str.substring(0, posA); | |
} | |
/** | |
* <p>Gets the substring before the last occurrence of a separator. | |
* The separator is not returned.</p> | |
* | |
* <p>A <code>null</code> string input will return <code>null</code>. | |
* An empty ("") string input will return the empty string. | |
* An empty or <code>null</code> separator will return the input string. | |
* | |
* StrUtils.beforeLast(null, *) = null | |
* StrUtils.beforeLast("", *) = "" | |
* StrUtils.beforeLast("abcba", "b") = "abc" | |
* StrUtils.beforeLast("abc", "c") = "ab" | |
* StrUtils.beforeLast("a", "a") = "" | |
* StrUtils.beforeLast("a", "z") = "a" | |
* StrUtils.beforeLast("a", null) = "a" | |
* StrUtils.beforeLast("a", "") = "a" | |
* | |
* @param str the String to get a substring from, may be null | |
* @param separator the String to search for, may be null | |
* @return the substring before the last occurrence of the separator, | |
* null if null String input | |
* @since 2.0 | |
*/ | |
public static String beforeLast(String str, String separator) { | |
if (isEmpty(str) || isEmpty(separator)) { | |
return str; | |
} | |
int pos = str.lastIndexOf(separator); | |
if (pos == -1) { | |
return str; | |
} | |
return str.substring(0, pos); | |
} | |
public static String between(String str, String before, String after) { | |
// Return a substring between the two strings. | |
int posA = str.indexOf(before); | |
if (posA == -1) { | |
return ""; | |
} | |
int posB = str.lastIndexOf(after); | |
if (posB == -1) { | |
return ""; | |
} | |
int adjustedPosA = posA + before.length(); | |
if (adjustedPosA >= posB) { | |
return ""; | |
} | |
return str.substring(adjustedPosA, posB); | |
} | |
/** | |
* Gets the substring after the first occurrence of a separator. | |
* The separator is not returned. | |
* | |
* A null string input will return null. | |
* An empty ("") string input will return the empty string. | |
* A null separator will return the empty string if the | |
* input string is not null. | |
* | |
* StrUtils.after(null, *) = null | |
* StrUtils.after("", *) = "" | |
* StrUtils.after(*, null) = "" | |
* StrUtils.after("abc", "a") = "bc" | |
* StrUtils.after("abcba", "b") = "cba" | |
* StrUtils.after("abc", "c") = "" | |
* StrUtils.after("abc", "d") = "" | |
* StrUtils.after("abc", "") = "abc" | |
* | |
* @param str the String to get a substring from, may be null | |
* @param separator the String to search for, may be null | |
* @return the substring after the first occurrence of the separator, | |
* null if null String input | |
* @since 2.0 | |
*/ | |
public static String after(String str, String separator) { | |
// Returns a substring containing all characters after a string. | |
int posA = str.lastIndexOf(separator); | |
if (posA == -1) { | |
return ""; | |
} | |
int adjustedPosA = posA + separator.length(); | |
if (adjustedPosA >= str.length()) { | |
return ""; | |
} | |
return str.substring(adjustedPosA); | |
} | |
/** | |
* Gets the substring after the last occurrence of a separator. | |
* The separator is not returned. | |
* | |
* A null string input will return null. | |
* An empty ("") string input will return the empty string. | |
* An empty or null separator will return the empty string if | |
* the input string is not null. | |
* | |
* StrUtils.afterLast(null, *) = null | |
* StrUtils.afterLast("", *) = "" | |
* StrUtils.afterLast(*, "") = "" | |
* StrUtils.afterLast(*, null) = "" | |
* StrUtils.afterLast("abc", "a") = "bc" | |
* StrUtils.afterLast("abcba", "b") = "a" | |
* StrUtils.afterLast("abc", "c") = "" | |
* StrUtils.afterLast("a", "a") = "" | |
* StrUtils.afterLast("a", "z") = "" | |
* | |
* @param str the String to get a substring from, may be null | |
* @param separator the String to search for, may be null | |
* @return the substring after the last occurrence of the separator, | |
* null if null String input | |
* @since 2.0 | |
*/ | |
public static String afterLast(String str, String separator) { | |
if (isEmpty(str)) { | |
return str; | |
} | |
if (isEmpty(separator)) { | |
return ""; | |
} | |
int pos = str.lastIndexOf(separator); | |
if (pos == -1 || pos == (str.length() - separator.length())) { | |
return ""; | |
} | |
return str.substring(pos + separator.length()); | |
} | |
/** | |
* <p>Overlays part of a String with another String.</p> | |
* | |
* <p>A <code>null</code> string input returns <code>null</code>. | |
* A negative index is treated as zero. | |
* An index greater than the string length is treated as the string length. | |
* The start index is always the smaller of the two indices.</p> | |
* | |
* StrUtils.overlay(null, *, *, *) = null | |
* StrUtils.overlay("", "abc", 0, 0) = "abc" | |
* StrUtils.overlay("abcdef", null, 2, 4) = "abef" | |
* StrUtils.overlay("abcdef", "", 2, 4) = "abef" | |
* StrUtils.overlay("abcdef", "", 4, 2) = "abef" | |
* StrUtils.overlay("abcdef", "zzzz", 2, 4) = "abzzzzef" | |
* StrUtils.overlay("abcdef", "zzzz", 4, 2) = "abzzzzef" | |
* StrUtils.overlay("abcdef", "zzzz", -1, 4) = "zzzzef" | |
* StrUtils.overlay("abcdef", "zzzz", 2, 8) = "abzzzz" | |
* StrUtils.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef" | |
* StrUtils.overlay("abcdef", "zzzz", 8, 10) = "abcdefzzzz" | |
* | |
* @param str the String to do overlaying in, may be null | |
* @param overlay the String to overlay, may be null | |
* @param start the position to start overlaying at | |
* @param end the position to stop overlaying before | |
* @return overlayed String, null if null String input | |
* @since 2.0 | |
*/ | |
public static String overlay(String str, String overlay, int start, int end) { | |
if (str == null) { | |
return null; | |
} | |
if (overlay == null) { | |
overlay = ""; | |
} | |
int len = str.length(); | |
if (start < 0) { | |
start = 0; | |
} | |
if (start > len) { | |
start = len; | |
} | |
if (end < 0) { | |
end = 0; | |
} | |
if (end > len) { | |
end = len; | |
} | |
if (start > end) { | |
int temp = start; | |
start = end; | |
end = temp; | |
} | |
return new StringBuffer(len + start - end + overlay.length() + 1) | |
.append(str.substring(0, start)) | |
.append(overlay) | |
.append(str.substring(end)) | |
.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment