com.hadoopz.MyDroidLib.util.JStringKit Maven / Gradle / Ivy
/*
* Copyright 2017 jw362j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hadoopz.MyDroidLib.util;
/**
*
* @author jw362j
*/
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.graphics.Paint;
import android.text.TextUtils;
import android.widget.TextView;
public class JStringKit {
public static final String EMPTY = "";
public static final int INDEX_NOT_FOUND = -1;
public static String firstCharToLowerCase(String str) {
Character firstChar = str.charAt(0);
String tail = str.substring(1);
str = Character.toLowerCase(firstChar) + tail;
return str;
}
public static String firstCharToUpperCase(String str) {
Character firstChar = str.charAt(0);
String tail = str.substring(1);
str = Character.toUpperCase(firstChar) + tail;
return str;
}
public static boolean isBlank(String str) {
return str == null || "".equals(str.trim());
}
public static boolean notBlank(String str) {
return str != null && !"".equals(str.trim());
}
public static boolean notBlank(String... strings) {
if (strings == null) {
return false;
}
for (String str : strings) {
if (str == null || "".equals(str.trim())) {
return false;
}
}
return true;
}
public static boolean notNull(Object... paras) {
if (paras == null) {
return false;
}
for (Object obj : paras) {
if (obj == null) {
return false;
}
}
return true;
}
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static boolean isNotEmpty(CharSequence cs) {
return !isEmpty(cs);
}
public static boolean isBlank(CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0 || cs.equals("null")) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(cs.charAt(i)) == false) {
return false;
}
}
return true;
}
public static boolean isNotBlank(CharSequence cs) {
return !isBlank(cs);
}
public static String trim(String str) {
return str == null ? null : str.trim();
}
public static String trimToNull(String str) {
String ts = trim(str);
return isEmpty(ts) ? null : ts;
}
public static String trimToEmpty(String str) {
return str == null ? EMPTY : str.trim();
}
public static boolean equals(CharSequence cs1, CharSequence cs2) {
return cs1 == null ? cs2 == null : cs1.equals(cs2);
}
public static boolean isNumber(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher match = pattern.matcher(str);
if (match.matches() == false) {
return false;
} else {
return true;
}
}
public static String hidePhoneNum(String phoneNum) {
return phoneNum.substring(0, 3) + "****" + phoneNum.substring(7, phoneNum.length());
}
public static String getUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
public static float getContentWidth(String content, TextView tView) {
if (null == tView && TextUtils.isEmpty(content)) {
return 0f;
}
return getContentWidthWithSize(content, tView.getTextSize()) * tView.getTextScaleX();
}
private static float getContentWidthWithSize(String content, float textSize) {
float width = 0f;
Paint tPaint = new Paint();
tPaint.setTextSize(textSize);
width = tPaint.measureText(content);
return width;
}
}