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

net.kut3.util.StringUtil Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2019 Kut3Net.
 *
 * 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 net.kut3.util;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 *
 */
public final class StringUtil {
    
    public static final Charset UTF_8 = StandardCharsets.UTF_8;
    
    private StringUtil() {
    }
    
    /**
     * 
     * @param bytes A bytes array to be encoded
     * @return A Base64 string
     */
    public static final String toBase64Str(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }
    
    /**
     * 
     * @param str The string to be checked
     * @return true or false
     */
    public static boolean isNullOrEmpty(String str) {
        return null == str || str.isEmpty();
    }
    
    /**
     * 
     * @param str The string to be checked
     * @return true or false
     */
    public static boolean isPhoneNo(String str) {
        if (10 != str.length()) {
            return false;
        }
        
        char[] phoneChars = str.toCharArray();
        if (phoneChars[0] != '0') {
            return false;
        }
        
        char secondChar = phoneChars[1];
        if ((secondChar != '3')
                && (secondChar != '5')
                && (secondChar != '7')
                && (secondChar != '8')
                && (secondChar != '9')
                ) {
            
            return false;
        }
        
        for (int i = 2; i < 10; i++) {
            if (!CharUtil.isNumber(phoneChars[i])) {
                return false;
            }
        }
        
        return true;
    }
    
    public static void main(String[] args) {
        System.out.println(isPhoneNo("09082808 6"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy