Created
December 11, 2015 20:51
-
-
Save theyann/3c495b2a302d3fb2f896 to your computer and use it in GitHub Desktop.
StringHelper is a utility class using a similar interface and proposing similar functionalities than commons lang StringUtils. Internally it is using Android system methods.
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 X.Y.Z; | |
import android.text.TextUtils; | |
import android.util.Base64; | |
/** | |
* Created by ylemin on 07/09/15. | |
* | |
* This class is similar in functionalities as the commons.lang.StringUtils, but using as much | |
* of the Android built-in tools and helpers as possible | |
*/ | |
public class StringHelper { | |
/** | |
* Constructors | |
*/ | |
private StringHelper() { | |
// should not be instantiated | |
} | |
/** | |
* Public Methods | |
*/ | |
public static boolean isNotBlank(CharSequence value) { | |
return !TextUtils.isEmpty(value); | |
} | |
public static boolean isBlank(CharSequence value) { | |
return TextUtils.isEmpty(value); | |
} | |
public static boolean isAnyBlank(CharSequence... values) { | |
for (int i = 0, count = values.length; i < count; i++) { | |
CharSequence value = values[i]; | |
if (isBlank(value)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public static boolean isNoneBlank(CharSequence... values) { | |
for (int i = 0, count = values.length; i < count; i++) { | |
CharSequence value = values[i]; | |
if (isBlank(value)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static boolean containsAnyToken(String input, String tokens, String tokenDelimiter) { | |
if (isAnyBlank(input, tokens)) { | |
return false; | |
} | |
if (isBlank(tokenDelimiter)) { | |
return input.contains(tokens); | |
} else { | |
String[] splitTokens = tokens.split(tokenDelimiter); | |
for (int i = 0, count = splitTokens.length; i < count; i++) { | |
if (input.contains(splitTokens[i])) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
public static int countOccurences(String input, char character) { | |
int counter = 0; | |
for (int i = 0; i < input.length(); i++) { | |
if (input.charAt(i) == character) { | |
counter++; | |
} | |
} | |
return counter; | |
} | |
public static String toBase64(String input) { | |
return Base64.encodeToString(input.getBytes(), Base64.DEFAULT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment