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

net.dongliu.commons.lang.Strings Maven / Gradle / Ivy

There is a newer version: 2.1.5
Show newest version
package net.dongliu.commons.lang;

/**
 * string utils methods
 *
 * @author Dong Liu
 */
public class Strings {

    /**
     * get sub string by begin(included) and end(not included) index.
     * begin and end can be negative, will be eval as str.length() + begin/end.
     * 
     *     sub("test", 1, 2) returns "e"
     *     sub("test", 1, -1) returns "es"
     *     sub("test", -2, -1) returns "s"
     *     sub(null, -2, -1) returns null
     * 
* * @param str can be null. when str is null, always return null * @return subString * @throws ArrayIndexOutOfBoundsException */ public static String sub(String str, int begin, int end) { if (str == null) { return null; } int len = str.length(); if (begin < 0) { begin += len; } if (end < 0) { end += len; } if (begin >= len || begin < 0) { throw new ArrayIndexOutOfBoundsException("invalid start index:" + begin); } if (end > len || end < 0) { throw new ArrayIndexOutOfBoundsException("invalid end index:" + end); } return str.substring(begin, end); } /** * get sub string by begin(included) index, till the end of string. * begin can be negative, will be eval as str.length() + begin. *
     *     sub("test", 1) returns "est"
     *     sub("test", -2) returns "st"
     *     sub(null, -2) returns null
     * 
* * @param str can be null. when str is null, always return null * @return subString * @throws ArrayIndexOutOfBoundsException */ public static String sub(String str, int begin) { return sub(str, begin, str.length()); } /** * get sub string by begin(included) index, till the end of string. * begin can be negative, will be eval as str.length() + begin. *
     *     rSub("test", 1) returns "t"
     *     rSub("test", -2) returns "te"
     *     rSub(null, -2) returns null
     * 
* * @param str can be null. when str is null, always return null * @return subString * @throws ArrayIndexOutOfBoundsException */ public static String rSub(String str, int end) { return sub(str, 0, end); } /** * get sub-string between begin and end. * * @param str if null, return null * @param begin the begin str, cannot be null * @param end the end str, cannot be null * @return str between begin and end. if not exists, return null */ public static String between(String str, String begin, String end) { if (str == null) { return null; } int idx = str.indexOf(begin); if (idx < 0) { return null; } idx += begin.length(); int eidx = str.indexOf(end, idx); if (eidx < 0) { return null; } return str.substring(idx, eidx); } /** * split str by white characters *
     * "a   b c" --> ["a", "b", "c"]
     * " a c " --> ["a","c"]
     * 
*/ public String[] split(String str) { return str.trim().split("\\w+"); } /** * format string *
{@code
     *     Strings.format("{}, {}", 1, "test") --> "1, test"
     *     Strings.format("{}, {}", 1) --> "1, {}"
     *     Strings.format("{}, {}", null, "test") --> "null, test"
     * }
     * 
* @param format if is null, return null */ public static String format(String format, Object... params) { if (format == null) { return null; } if (format.isEmpty()) { return ""; } StringBuilder sb = new StringBuilder(); int count = 0; int state = 0; for (int idx = 0; idx < format.length(); idx++) { char c = format.charAt(idx); switch (c) { case '{': if (state == 0) { state = 1; } else { sb.append('{'); } break; case '}': if (state == 1) { if (count < params.length) { Object param = params[count++]; sb.append(param); state = 0; } else { sb.append("{}"); state = 0; } } else { sb.append('}'); } break; default: sb.append(c); } } return sb.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy