Last active
September 6, 2017 07:44
-
-
Save zacscoding/7da523ab5814b6caa8722a3ead4f8b1f to your computer and use it in GitHub Desktop.
Java Utils - StringUtil
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 com.zaccoding.util | |
public class StringUtil { | |
public static boolean isEmpty(String value) { | |
return (value == null) || (value.length() == 0); | |
} | |
public static boolean isNotEmpty(String value) { | |
return (value != null) && (value.length() > 0); | |
} | |
public static boolean isBlank(String value) { | |
int strLen; | |
if (value == null || (strLen = value.length()) == 0) { | |
return true; | |
} | |
for (int i = 0; i < strLen; i++) { | |
if (!Character.isWhitespace(value.charAt(i))) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static String combineStrings(boolean threadSafe, String... values) { | |
if (values == null || values.length == 0) | |
return null; | |
int totalLen = 0; | |
for (String value : values) { | |
totalLen += getStringLengthWith(value, 0); | |
} | |
if (threadSafe) { | |
StringBuffer sb = new StringBuffer(totalLen); | |
for (String value : values) { | |
sb.append(value); | |
} | |
return sb.toString(); | |
} | |
else { | |
StringBuilder sb = new StringBuilder(totalLen); | |
for (String value : values) { | |
sb.append(value); | |
} | |
return sb.toString(); | |
} | |
} | |
public static int getStringLength(String value, int defaultLength) { | |
if (value == null) { | |
return defaultLength; | |
} | |
return value.length(); | |
} | |
public static String convertHTML(String text) { | |
final int initSize = text.length() * 2; | |
StringBuilder sb = new StringBuilder(initSize); | |
for (int i = 0; i < text.length(); i++) { | |
char c = text.charAt(i); | |
switch (c) { | |
case '<': | |
sb.append("<"); | |
break; | |
case '>': | |
sb.append(">"); | |
break; | |
case '&': | |
sb.append("&"); | |
break; | |
case '"': | |
sb.append("""); | |
break; | |
case '\'': | |
sb.append("'"); | |
break; | |
default: | |
sb.append(c); | |
} | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment