All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hadoopz.MyDroidLib.util.JStringKit Maven / Gradle / Ivy

There is a newer version: 1.0.62
Show newest version
/*
 * 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;

/**
 * Android操作字符串工具类
 *
 * @author blue
 *
 */
public class JStringKit {

    /**
     * The empty String {@code ""}.
     *
     * @since 2.0
     */
    public static final String EMPTY = "";

    /**
     * Represents a failed index search.
     *
     * @since 2.1
     */
    public static final int INDEX_NOT_FOUND = -1;

    /**
     * 首字母变小写
     *
     * @param str
     * @return
     */
    public static String firstCharToLowerCase(String str) {
        Character firstChar = str.charAt(0);
        String tail = str.substring(1);
        str = Character.toLowerCase(firstChar) + tail;
        return str;
    }

    /**
     * 首字母变大写
     *
     * @param str
     * @return
     */
    public static String firstCharToUpperCase(String str) {
        Character firstChar = str.charAt(0);
        String tail = str.substring(1);
        str = Character.toUpperCase(firstChar) + tail;
        return str;
    }

    /**
     * 字符串为 null 或者为 "" 时返回 true
     *
     * @param str
     * @return
     */
    public static boolean isBlank(String str) {
        return str == null || "".equals(str.trim());
    }

    /**
     * 字符串不为 null 而且不为 "" 时返回 true
     *
     * @param str
     * @return
     */
    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;
    }

    /**
     * 

* Checks if a CharSequence is empty ("") or null. *

* *
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * 
* *

* NOTE: This method changed in Lang version 2.0. It no longer trims the * CharSequence. That functionality is available in isBlank(). *

* * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is empty or null * @since 3.0 Changed signature from isEmpty(String) to * isEmpty(CharSequence) */ public static boolean isEmpty(CharSequence cs) { return cs == null || cs.length() == 0; } /** *

* Checks if a CharSequence is not empty ("") and not null. *

* *
     * StringUtils.isNotEmpty(null)      = false
     * StringUtils.isNotEmpty("")        = false
     * StringUtils.isNotEmpty(" ")       = true
     * StringUtils.isNotEmpty("bob")     = true
     * StringUtils.isNotEmpty("  bob  ") = true
     * 
* * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is not empty and not null * @since 3.0 Changed signature from isNotEmpty(String) to * isNotEmpty(CharSequence) */ public static boolean isNotEmpty(CharSequence cs) { return !isEmpty(cs); } /** *

* Checks if a CharSequence is whitespace, empty ("") or null. *

* *
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("null")    = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * 
* * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is null, empty or whitespace * @since 2.0 * @since 3.0 Changed signature from isBlank(String) to * isBlank(CharSequence) */ 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; } /** *

* Checks if a CharSequence is not empty (""), not null and not whitespace * only. *

* *
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("null")    = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * 
* * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is not empty and not null and * not whitespace * @since 2.0 * @since 3.0 Changed signature from isNotBlank(String) to * isNotBlank(CharSequence) */ public static boolean isNotBlank(CharSequence cs) { return !isBlank(cs); } /** *

* Removes control characters (char <= 32) from both ends of this String, * handling {@code null} by returning {@code null}. *

* *

* The String is trimmed using {@link String#trim()}. Trim removes start and * end characters <= 32. To strip whitespace use {@link #strip(String)}. *

* *

* To trim your choice of characters, use the {@link #strip(String, String)} * methods. *

* *
     * StringUtils.trim(null)          = null
     * StringUtils.trim("")            = ""
     * StringUtils.trim("     ")       = ""
     * StringUtils.trim("abc")         = "abc"
     * StringUtils.trim("    abc    ") = "abc"
     * 
* * @param str the String to be trimmed, may be null * @return the trimmed string, {@code null} if null String input */ public static String trim(String str) { return str == null ? null : str.trim(); } /** *

* Removes control characters (char <= 32) from both ends of this String * returning {@code null} if the String is empty ("") after the trim or if * it is {@code null}. * *

* The String is trimmed using {@link String#trim()}. Trim removes start and * end characters <= 32. To strip whitespace use * {@link #stripToNull(String)}. *

* *
     * StringUtils.trimToNull(null)          = null
     * StringUtils.trimToNull("")            = null
     * StringUtils.trimToNull("     ")       = null
     * StringUtils.trimToNull("abc")         = "abc"
     * StringUtils.trimToNull("    abc    ") = "abc"
     * 
* * @param str the String to be trimmed, may be null * @return the trimmed String, {@code null} if only chars <= 32, empty or * null String input * @since 2.0 */ public static String trimToNull(String str) { String ts = trim(str); return isEmpty(ts) ? null : ts; } /** *

* Removes control characters (char <= 32) from both ends of this String * returning an empty String ("") if the String is empty ("") after the trim * or if it is {@code null}. * *

* The String is trimmed using {@link String#trim()}. Trim removes start and * end characters <= 32. To strip whitespace use * {@link #stripToEmpty(String)}. *

* *
     * StringUtils.trimToEmpty(null)          = ""
     * StringUtils.trimToEmpty("")            = ""
     * StringUtils.trimToEmpty("     ")       = ""
     * StringUtils.trimToEmpty("abc")         = "abc"
     * StringUtils.trimToEmpty("    abc    ") = "abc"
     * 
* * @param str the String to be trimmed, may be null * @return the trimmed String, or an empty String if {@code null} input * @since 2.0 */ public static String trimToEmpty(String str) { return str == null ? EMPTY : str.trim(); } // Equals // ----------------------------------------------------------------------- /** *

* Compares two CharSequences, returning {@code true} if they are equal. *

* *

* {@code null}s are handled without exceptions. Two {@code null} references * are considered to be equal. The comparison is case sensitive. *

* *
     * StringUtils.equals(null, null)   = true
     * StringUtils.equals(null, "abc")  = false
     * StringUtils.equals("abc", null)  = false
     * StringUtils.equals("abc", "abc") = true
     * StringUtils.equals("abc", "ABC") = false
     * 
* * @see java.lang.String#equals(Object) * @param cs1 the first CharSequence, may be null * @param cs2 the second CharSequence, may be null * @return {@code true} if the CharSequences are equal, case sensitive, or * both {@code null} * @since 3.0 Changed signature from equals(String, String) to * equals(CharSequence, CharSequence) */ public static boolean equals(CharSequence cs1, CharSequence cs2) { return cs1 == null ? cs2 == null : cs1.equals(cs2); } /** * 数字正则表达式 * * @param str * @return */ 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; } } /** * 隐藏电话号码中间四位 * * @param phoneNum * @return */ public static String hidePhoneNum(String phoneNum) { return phoneNum.substring(0, 3) + "****" + phoneNum.substring(7, phoneNum.length()); } /** * 生成唯一标示符 * * @author andrew * @return */ 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; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy