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: 1.0.9
Show newest version
package net.dongliu.commons.lang;

import java.util.Collection;

/**
 * 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) { if (str == null) { return null; } return str.substring(begin, str.length()); } /** * 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+"); } /** * join str *
     * join(",","b", "c") --> "b,c"
     * 
* * @return string */ public static String join(String separator, String... strings) { StringBuilder sb = new StringBuilder(); int total = strings.length; int i = 0; for (String str : strings) { sb.append(str); if (i++ < total - 1) { sb.append(separator); } } return sb.toString(); } /** * join strings with separator * * @param strings not null * @param separator not null * @return string not null */ public static String join(String separator, Collection strings) { StringBuilder sb = new StringBuilder(); int total = strings.size(); int i = 0; for (String str : strings) { sb.append(str); if (i++ < total - 1) { sb.append(separator); } } return sb.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy