io.uengine.util.StringUtils.java Source code

Java tutorial

Introduction

Here is the source code for io.uengine.util.StringUtils.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 io.uengine.util;

import com.google.common.base.Joiner;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;

/**
 * String Utility.
 *
 * @author Byoung Gon, Kim
 * @since 0.1
 */
public class StringUtils {

    /**
     * Properties? key value ? .
     *
     * @param properties Properties
     * @return key value ?
     */
    public static String propertiesToString(Properties properties) {
        StringWriter writer = new StringWriter();
        PrintWriter out = new PrintWriter(writer);
        Set<Object> keys = properties.keySet();
        for (Object key : keys) {
            out.println(key + "=" + properties.get(key));
        }
        return writer.getBuffer().toString();
    }

    /**
     * ?  ?  .
     *
     * @param values ? 
     * @return ? 
     */
    public static String[] collectionToStringArray(List<String> values) {
        String[] array = new String[values.size()];
        int index = 0;
        for (String value : values) {
            array[index] = value;
            index++;
        }
        return array;
    }

    /**
     *  ??  ?.
     *
     * @param str  ?
     * @return  <tt>true</tt>
     */
    public static boolean isEmpty(String str) {
        return (str == null || str.trim().length() < 1);
    }

    /**
     *  ?  ? ?? .
     *
     * @param values 
     * @return  ? ? ?
     */
    public static String collectionToCommaDelimitedString(List<String> values) {
        return collectionToDelimitedString(values, ",");
    }

    /**
     *  ?  ? ? .
     *
     * @param values    
     * @param delimiter ?
     * @return ? ? ?
     */
    public static String collectionToDelimitedString(List<String> values, String delimiter) {
        return Joiner.on(delimiter).join(values);
    }

    /**
     * ? ?  .
     *
     * @param values ? 
     * @return ? 
     */
    public static List<String> arrayToCollection(String[] values) {
        List<String> list = new ArrayList<String>(values.length);
        Collections.addAll(list, values);
        return list;
    }

    /**
     * ?   ??  .
     *
     * @param value     ? ? 
     * @param separator ?
     * @return ? 
     */
    public static List<String> stringToCollection(String value, String separator) {
        String[] strings = org.apache.commons.lang.StringUtils.splitPreserveAllTokens(value, separator);
        return arrayToCollection(strings);
    }

    /**
     *  ?? ?   ? ?.
     * <p>
     * <pre>
     * String result = StringUtils.replace("Hello World", "Hello", "World"); // "World World"
     * </pre>
     * </p>
     *
     * @param inString    ?
     * @param oldPattern  ? 
     * @param newPattern  ? ? 
     * @return ?? ?
     */
    public static String replace(String inString, String oldPattern, String newPattern) {
        return org.springframework.util.StringUtils.replace(inString, oldPattern, newPattern);
    }

    /**
     * ?? Prefix .
     *
     * @param message ?
     * @param prefix   Prefix
     * @param isTrim  Prefix  ?? Trim  
     * @return Prefix  ?
     */
    public static String removePrefix(String message, String prefix, boolean isTrim) {
        if (message.contains(prefix)) {
            int start = message.indexOf(prefix) + prefix.length();
            return isTrim ? message.substring(start).trim() : message.substring(start);
        }
        return message;
    }

    /**
     * Escape  ?? unescape.
     *
     * @param string Escape  ?
     * @return escape Unescape  ?
     */
    public static String unescape(String string) {
        StringBuilder builder = new StringBuilder();
        builder.ensureCapacity(string.length());
        int lastPos = 0, pos = 0;
        char ch;
        while (lastPos < string.length()) {
            pos = string.indexOf("%", lastPos);
            if (pos == lastPos) {
                if (string.charAt(pos + 1) == 'u') {
                    ch = (char) Integer.parseInt(string.substring(pos + 2, pos + 6), 16);
                    builder.append(ch);
                    lastPos = pos + 6;
                } else {
                    ch = (char) Integer.parseInt(string.substring(pos + 1, pos + 3), 16);
                    builder.append(ch);
                    lastPos = pos + 3;
                }
            } else {
                if (pos == -1) {
                    builder.append(string.substring(lastPos));
                    lastPos = string.length();
                } else {
                    builder.append(string.substring(lastPos, pos));
                    lastPos = pos;
                }
            }
        }
        return builder.toString();
    }

    /**
     *  ?? escape .
     *
     * @param string Escape  ?
     * @return escape  ?
     */
    public static String escape(String string) {
        int i;
        char j;
        StringBuilder builder = new StringBuilder();
        builder.ensureCapacity(string.length() * 6);
        for (i = 0; i < string.length(); i++) {
            j = string.charAt(i);
            if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j))
                builder.append(j);
            else if (j < 256) {
                builder.append("%");
                if (j < 16)
                    builder.append("0");
                builder.append(Integer.toString(j, 16));
            } else {
                builder.append("%u");
                builder.append(Integer.toString(j, 16));
            }
        }
        return builder.toString();
    }

    /**
     *  ?? base64 ?
     *
     * @param string Escape  ?
     * @return escape  ?
     */
}