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

com.datastax.util.lang.StringUtil Maven / Gradle / Ivy

package com.datastax.util.lang;

import com.datastax.collection.KeyValue;

import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringUtil {

    private static final char DY = '\'';
    private static final char DH = ',';
    private static int[] filter = new int[128];
    private static int[] filterEnd = new int[128];
    static {
        filter['<'] = Integer.MAX_VALUE / 2;
        filterEnd['<'] = '>';

        filter['&'] = 10;
        filterEnd['&'] = ';';

        filter[';'] = -1;
        filter['\n'] = -1;

        filter['\r'] = -1;
        filter['\t'] = -1;
        filter[' '] = 1;
        filter['*'] = 1;
        filter['-'] = 1;
        filter['.'] = 1;
        filter['#'] = 1;

    }

    /**
     * 去除html标签
     *
     * @param input
     * @return
     */
    public static String rmHtmlTag(String input) {
        if (isBlank(input)) {
            return "";
        }
        int length = input.length();
        int tl = 0;
        StringBuilder sb = new StringBuilder();
        char c = 0;
        for (int i = 0; i < length; i++) {
            c = input.charAt(i);

            if (c > 127) {
                sb.append(c);
                continue;
            }

            switch (filter[c]) {
                case -1:
                    break;
                case 0:
                    sb.append(c);
                    break;
                case 1:
                    if (sb.length() > 0 && sb.charAt(sb.length() - 1) != c)
                        sb.append(c);
                    do {
                        i++;
                    } while (i < length && input.charAt(i) == c);

                    if (i < length || input.charAt(length - 1) != c)
                        i--;
                    break;
                default:
                    tl = filter[c] + i;
                    int tempOff = i;
                    boolean flag = false;
                    char end = (char) filterEnd[c];
                    for (i++; i < length && i < tl; i++) {
                        c = input.charAt(i);
                        if (c > 127)
                            continue;
                        if (c == end) {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        i = tempOff;
                        sb.append(input.charAt(i));
                    }
                    break;
            }
        }
        return sb.toString();
    }

    /**
     * 判断字符串是否为空
     *
     * @param cs
     * @return
     */
    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断字符串是否不为空
     *
     * @param cs
     * @return
     */
    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);

    }

    public static String makeSqlInString(String str) {
        String[] strs = str.split(",");
        StringBuilder sb = new StringBuilder();
        String field = null;
        for (int i = 0; i < strs.length; i++) {
            field = strs[i].trim();
            if (isNotBlank(field)) {
                sb.append(DY);
                sb.append(field);
                sb.append(DY);
                if (i < strs.length - 1) {
                    sb.append(DH);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 将一个字符串.转换成排序后的字符数组
     *
     * @param str
     * @return
     */
    public static char[] sortCharArray(String str) {
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        return chars;
    }

    public static boolean isBlank(char[] chars) {
        // TODO Auto-generated method stub
        int strLen;
        if (chars == null || (strLen = chars.length) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(chars[i]) == false) {
                return false;
            }
        }
        return true;
    }

    public static String substring(String text, String startText, String endText) {
        int start = text.indexOf(startText);
        if (start < 0)
            return null;
        start += startText.length();
        int end = text.indexOf(endText);
        if (end < 0)
            return null;

        return text.substring(start, end);
    }

    public static String substringLast(String text, String startText, String endText) {
        int start = text.lastIndexOf(startText);
        if (start < 0)
            return null;
        start += startText.length();
        int end = text.lastIndexOf(endText);
        if (end < 0)
            return null;

        return text.substring(start, end);
    }

    public static String substringIndent(String text, String startText, String endText) {
        int start = text.indexOf(startText);
        if (start < 0)
            return null;
        start += startText.length();
        int end = text.indexOf(endText, start);
        if (end < 0)
            return null;

        return text.substring(start, end);
    }

    public static String substringIndent(String text,String[][] patternGroups) {
        for(String[] pattern : patternGroups) {
            String startText=pattern[0];
            String endText=pattern[1];

            int start = text.indexOf(startText);
            if (start < 0)
                return null;
            start += startText.length();
            int end = text.indexOf(endText, start);
            if (end < 0)
                return null;

            if(end>start) {
                return text.substring(start, end);
            }
        }
        return null;
    }

    public static void substringAll(String text, String startText, String endText, List valueList) {
        int start = text.indexOf(startText);
        if (start < 0)
            return;
        start += startText.length();
        int end = text.indexOf(endText, start);

        String ret = null;
        if (end < 0) {
            ret = text.substring(start);
            valueList.add(ret);
            return;
        } else {
            ret = text.substring(start, end);
            text = text.substring(end);
        }

        valueList.add(ret);

        substringAll(text, startText, endText, valueList);
    }

    public static String substringIndentLast(String text, String startText, String endText) {
        int start = text.lastIndexOf(startText);
        if (start < 0)
            return null;
        start += startText.length();
        int end = text.indexOf(endText, start);
        if (end < 0)
            return null;

        return text.substring(start, end);
    }

    public static String substringFirstAndLast(String text,String startText,String endText){
        int start=text.indexOf(startText);
        int end=text.lastIndexOf(endText);
        if(end-start<=1){
            return null;
        }else {
            return text.substring(start+1,end);
        }
    }

    public static String streamToString(InputStream is) {
        return streamToString(is, "utf-8");
    }

    public static String objToString(Object obj) {
        if (obj != null) {
            return obj.toString();
        }
        return null;
    }

    public static String streamToString(InputStream is,String encoding,boolean newLine){
        String ret=null;
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(is,encoding));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = in.readLine()) != null){
                buffer.append(line);
                if(newLine) buffer.append("\n");
            }
            ret=buffer.toString();
        }catch (Exception ex){

        }
        return ret;
    }

    public static String streamToString(InputStream is, String encoding) {
        if (is == null) {
            return null;
        }
        String ret = null;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(is, encoding));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = in.readLine()) != null) {
                buffer.append(line);
            }
            ret = buffer.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ret;
    }

    public static InputStream stringToStream(String text) {
        return stringToStream(text, "utf-8");
    }

    public static InputStream stringToStream(String text, String encoding) {
        try {
            return new ByteArrayInputStream(text.getBytes(encoding));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    public static boolean containsNum(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public static boolean isEng(String str) {
        char[] chars=str.toCharArray();
        boolean isPhontic = false;
        for(int i = 0; i < chars.length; i++) {
            isPhontic = (chars[i] >= 'a' && chars[i] <= 'z') || (chars[i] >= 'A' && chars[i] <= 'Z');
            if (!isPhontic) {
                return false;
            }
        }
        return true;
    }

    public static boolean containsEng(String str) {
        char[] chars=str.toCharArray();
        boolean isPhontic = false;
        for(int i = 0; i < chars.length; i++) {
            isPhontic = (chars[i] >= 'a' && chars[i] <= 'z') || (chars[i] >= 'A' && chars[i] <= 'Z');
            if (isPhontic) {
                return true;
            }
        }
        return false;
    }

    public static double formatDecimal(double d, String format) {
        DecimalFormat df = new DecimalFormat(format);
        return Double.parseDouble(df.format(d));
    }

    public static String formatJScript(String html) {
        if (null == html) return html;

        html = html.replace("\\r", "");
        html = html.replace("\\n", "");
        html = html.replace("\\t", "");
        html = html.replace("\\\"", "\"");
        html = html.replace("\\/", "/");

        return html;
    }

    public static  String listToString(List list, String sep) {
        StringBuffer sb = new StringBuffer();
        for (T str : list) {
            sb.append(str + sep);
        }
        String ret = sb.toString();
        if (ret.length() > 0) ret = ret.substring(0, ret.length() - 1);
        return ret;
    }

    public static  String listToString(List list, String sep,String surrounder) {
        StringBuffer sb = new StringBuffer();
        for (T str : list) {
            sb.append(surrounder+str +surrounder+ sep);
        }
        String ret = sb.toString();
        if (ret.length() > 0) ret = ret.substring(0, ret.length() - 1);
        return ret;
    }

    public static  String arrayToString(T[] list, String sep) {
        StringBuffer sb = new StringBuffer();
        for (T str : list) {
            sb.append(str + sep);
        }
        String ret = sb.toString();
        if (ret.length() > 0) ret = ret.substring(0, ret.length() - 1);
        return ret;
    }

    public static String replace ( String string, String pattern, String replacement ) {
        String replaced = null;

        if (string == null) {
            replaced = null;
        } else if (pattern == null || pattern.length() == 0 ) {
            replaced = string;
        } else {

            StringBuffer sb = new StringBuffer();

            int lastIndex = 0;
            int index = string.indexOf(pattern);
            while (index >= 0) {
                sb.append(string.substring(lastIndex, index));
                sb.append(replacement);
                lastIndex = index + pattern.length();
                index = string.indexOf(pattern, lastIndex);
            }
            sb.append(string.substring(lastIndex));
            replaced = sb.toString();
        }
        return replaced;
    }

    public static String replace ( String string, String pattern, String replacement, int start ) {
        String begin = string.substring(0, start);
        String end = string.substring(start);
        return begin + replace(end, pattern, replacement );
    }

    public static int countWord(String text,String word){
        int counter=0;
        int index=0;
        while (index>=0){
            index=text.indexOf(word);
            if(index>=0){
                counter++;
                text=text.substring(index+word.length());
            }
        }
        return counter;
    }

    public static boolean inList(String[] list,String text){
        for(String word : list){
            if(word.equals(text)){
                return true;
            }
        }
        return false;
    }

    public static boolean inList(List list,String text){
        for(String word : list){
            if(word.equals(text)){
                return true;
            }
        }
        return false;
    }

    public static long extractNum(String text){
        StringBuffer sb=new StringBuffer();
        if (text == null) {
            return -1;
        }
        int sz = text.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(text.charAt(i)) == true) {
                sb.append(text.charAt(i));
            }
        }
        return Long.parseLong(sb.toString());
    }

    public static String trim(String text){
        if(text==null) return null;
        return text.trim().replace("\r\n","").replace("\n","").replace(" ","").replace("\t","");
    }

    //移除两个BLOCK之间的内容
    public static String removeBlock(String text,String startText,String endText,boolean keepBlock){
        int start = text.indexOf(startText);
        if (start < 0)
            return text;
        start += startText.length();
        int end = text.indexOf(endText, start);
        if (end < 0)
            return text;

        StringBuffer sb=new StringBuffer();

        if(keepBlock){
            sb.append(text.substring(0,start-startText.length()));
            sb.append(text.substring(end));
        }else {
            sb.append(text.substring(0,start));
            sb.append(text.substring(end+endText.length()));
        }
        return sb.toString();
    }

    public static List> split(String text,List dicts){
        List> tList=new ArrayList<>();
        tList.add(new KeyValue<>(text,false));
        for(String dict : dicts){
            tList=split(tList,dict);
        }
        return tList;
    }

    private static List> split(List> wList,
                                                        String dict){
        List> list=new ArrayList<>();
        for(KeyValue keyValue : wList){
            String key=keyValue.key();
            boolean value=keyValue.value();
            if(value){
                list.add(new KeyValue<>(key,value));
            }else {
                String[] texts=key.split(dict);

                if(texts.length>1) {
                    for (String t : texts) {
                        if(t.length()>0) {
                            list.add(new KeyValue<>(t, false));
                        }
                        list.add(new KeyValue<>(dict, true));
                    }
                    list.remove(list.size()-1);
                }else if(texts.length>0) {
                    if(texts[0]!=null) {
                        //分隔符的词在最后
                        if ((texts[0] + dict).equals(key)) {
                            list.add(new KeyValue<>(texts[0], false));
                            list.add(new KeyValue<>(dict, true));
                        } else {
                            list.add(new KeyValue<>(key, value));
                        }
                    }
                }
            }
        }
        return list;
    }

    public static void main(String[] args){
        test1();
    }

    private static void test1(){
        String text="Face旷世的A轮投资方投了微信的B轮、腾讯的C轮、阿里的B轮。";
        List dicts=new ArrayList<>();
        dicts.add("A轮");
        dicts.add("B轮");
        dicts.add("Face旷世");
        List> list=split(text,dicts);
        System.out.println(list);
    }

    private static void test2(){
        String text="真格基金的A轮";
        List dicts=new ArrayList<>();
        dicts.add("A轮");
        dicts.add("真格基金");
        List> list=split(text,dicts);
        System.out.println(list);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy