QQ入力法の詩人のモードをまねて文字列をフォーマットします

7948 ワード

擬QQ入力法-詩人モード-フォーマット文字列
コードに直接行きましょう.何も言うことはありません.
キー:列ごとの高さを計算する方法
package ;

import android.content.Context;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * createTime: 2020/3/19.11:26
 * updateTime: 2020/3/19.11:26
 * author: singleMan.
 * desc:           
 */
public class BardFormatUtil {

    private static final int MAX_ROW_LENGTH = 7;//            

    public static String format(Context context, String input){
        if(input.contains(" ")){
            return input;
        }
        input = input.replace("
", "").trim(); String oneSymbolStr = context.getString(R.string.a4); StringBuilder sb =new StringBuilder(); List> lists = new ArrayList<>(); // , if(hasSymbol(input) && !hasConsecutiveSymbol(input)){ List bdSplit = splitBySymbol(input); for(String s : bdSplit){ List item = new ArrayList<>(); for(char c : s.toCharArray()){ item.add(String.valueOf(c)); } lists.add(item); } }else { char[] chars = input.toCharArray(); int length = chars.length; List mList = new ArrayList<>(); for(char c : chars){ mList.add(String.valueOf(c)); } // int x = smartCompute(length); while (Math.ceil(length / x) > MAX_ROW_LENGTH){ x += smartCompute(Math.ceil(length / x)); } System.out.println("Input:"+input+" :"+x); List> list = mSubList(mList, x); lists.addAll(list); } int maxLength = lists.get(0).size(); for (int i = 0; i < lists.size(); i++){ if (maxLength < lists.get(i).size()){ maxLength = lists.get(i).size(); } } for(int a=0;a < maxLength; a++){ for(int i = lists.size()-1; i>=0; i--){ List zu = lists.get(i); if(a < zu.size()){ String s = zu.get(a); switch (hasSymbolCount(s.charAt(0))){ case 1: sb.append(oneSymbolStr); sb.append(oneSymbolStr); sb.append(s); sb.append(oneSymbolStr); break; case 2: sb.append(oneSymbolStr); sb.append(s); sb.append(oneSymbolStr); break; case 4: sb.append(s); break; } if(i > 0) { sb.append(" "); } }else { sb.append(" "+" "); } } if(a < maxLength -1) { sb.append("
"); } } System.out.println(" :
"+sb.toString()); System.out.println("---------------------------"); return sb.toString(); } // private static int smartCompute(double totalLength){ if(totalLength <= 5d){ return 1; } if(totalLength % 5d == 0d){ return (int) Math.ceil(totalLength / 5d); } if(totalLength % 7d == 0d){ return (int) Math.ceil(totalLength / 7d); } return 2; } /** * n list * @param source * @param n * @return */ private static List> mSubList(List source, int n) { List> result = new ArrayList>(); int oneSize = (int) Math.ceil(Double.valueOf(source.size()) / Double.valueOf(n)); System.out.println(" :"+oneSize); for(int i = 0;i < n; i++){ int start = i * oneSize; int end = (i+1) * oneSize; if(end <= source.size()){ result.add(source.subList(start,end)); }else { result.add(source.subList(start,source.size())); } } return result; } /** * :1/4 * @param c * @return */ private static int hasSymbolCount(char c){ if (Character.getType(c) == Character.OTHER_LETTER || isChinesePunctuation(c)) { return 4; } else if (Character.isDigit(c)) { return 2; } else if (Character.isLetter(c)) { return 2; } else { return 1; } } /** * * @param str * @return */ private static boolean hasConsecutiveSymbol(String str){ List indexList = takeOutSymbol(str); for(int i = 0;i < indexList.size()-1;++i){ System.out.println(" ::"+(indexList.get(i+1) - indexList.get(i))); if(indexList.get(i+1) - indexList.get(i) == 1){ System.out.println(" "); return true; } } return false; } /** * * @param str * @return */ private static List takeOutSymbol(String str){ List list = new ArrayList<>(); int index = 0; for(char c : str.toCharArray()){ if(!(Character.getType(c) == Character.OTHER_LETTER) && !(Character.isDigit(c)) && !(Character.isLetter(c))){ list.add(index+1); } index++; } if(!list.contains(str.length())){ list.add(str.length()); } System.out.println(JSON.toJSONString(list)); return list; } // char private static boolean isChinesePunctuation(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS || ub == Character.UnicodeBlock.VERTICAL_FORMS) { return true; } else { return false; } } /** * * @param s * @return */ private static boolean hasSymbol(String s) { boolean b = false; String tmp = s; tmp = tmp.replaceAll("\\p{P}", ""); if (s.length() != tmp.length()) { b = true; } return b; } /** * * @param str * @return */ private static List splitBySymbol(String str){ StringBuilder sb = new StringBuilder(str); List splitSymbolList = takeOutSymbol(str); List list = new ArrayList<>(); if(splitSymbolList.size() == 0){ list.add(str); return list; } int def = 0; for(int index : splitSymbolList){ int indexStart = def; int indexEnd = index; def = indexEnd; String substring = sb.substring(indexStart, indexEnd); System.out.println("add:"+substring); list.add(substring); } return list; } }