
com.github.becausetesting.lang.StringUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
A common libraries used for testing framework.
package com.github.becausetesting.lang;
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class StringUtils {
/**
* Determines if the string is null or empty.
*
* @param s
* The string to test
* @return true if the string is null or empty.
*/
public static boolean isNullOrEmpty(String s) {
return s == null || s.trim().length() == 0;
}
private String byte2String(byte[] bytes){
return new String(bytes);
}
public byte[] string2Bytes(String str) {
return str.getBytes(Charset.forName("UTF-8"));
}
public char[] String2Array(String str) {
return str.toCharArray();
}
public String stringFormat(String str, String format) {
return String.format(format, str);
}
public String getRandomString(int len) {
String result = "";
String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // 9
int n = alphabet.length(); // 10
for (int k = 0; k < len; k++) {
result = result + alphabet.charAt(new SecureRandom().nextInt(n)); // 13
}
return result;
}
public List split(String str, String seperate) {
List list=new ArrayList();
StringTokenizer stringTokenizer = new StringTokenizer(str, seperate);
int count = 0;
while (stringTokenizer.hasMoreTokens()) {
String seperateStr = stringTokenizer.nextToken();
list.add(seperateStr);
}
return list;
}
/*
* StringBuilder >1.5 StringBuffer before StringBuilder StringBuilder not
* thread safe no synchronized
*
*
* StringBuilder is faster than StringBuffer if not mulitply thread use
* StringBuffer
*
* String
© 2015 - 2025 Weber Informatics LLC | Privacy Policy