按位长度进行字符串的分割输出,长度不足补0 金融金额的三分位算法

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 
  4 public class NiukeTest {
  5 
  6     public static void main(String[] args) {
  8         List<String> strList=getSpilitStrList("1847519751890554548999");
  9         System.out.println(strList);
 11     }
 12 
 13     /**
 14      * 按8位长度进行字符串的分割输出,长度不足补0 代码中的数字8改为3即可
      *
 15      * @param srcStr
 16      * @return
 17      */
 18     public static String getSpilitStr(String srcStr){
 19         if ("".equals(srcStr.trim())){
 20             return null;
 21         }
 22         if (srcStr.length()>100){
 23             System.out.println("输入字符串过长!超过长度100");
 24             return null;
 25         }
 27         // helloworldiamwanzhongliao
 28         StringBuilder stringBuilder = new StringBuilder();
 29         boolean flag=false;
 30         String srcStrCopy=srcStr;
 31         int count=1;
 32         for (int i = 0; i < srcStr.length(); i++) {
 33             char charAt = srcStr.charAt(i);
 34             stringBuilder.append(charAt);
 35             if (flag){
 36                 if (stringBuilder.toString().contains(" ")){
 37                     String[] strings = stringBuilder.toString().trim().split("\\s");
 38                     StringBuilder stringBuilder1 = new StringBuilder();
 39                     for (int k = 0; k < strings.length; k++) {
 40                         stringBuilder1.append(strings[k]);
 41                     }
 42                     int length = stringBuilder1.length();
 43                     if (length%8==0){
 44                         String substring = srcStrCopy.substring(count*8);
 45                         srcStr=substring;
 46                         i=-1;
 47                         stringBuilder.append(" ");
 48                         count++;
 49 
 50                         if (srcStr.length()/8<1){
 51                             StringBuilder stringBuilder4 = new StringBuilder();
 52                             stringBuilder4.append(srcStr);
 53                             for (int j = 0; j < 8-srcStr.length(); j++) {
 54                                 stringBuilder4.append("0");
 55                             }
 56                             stringBuilder.delete(stringBuilder.toString().lastIndexOf(" "),stringBuilder.toString().length());
 57                             stringBuilder.append(" ");
 58                             stringBuilder.append(stringBuilder4.toString());
 59                             break;
 60                         }
 61                         continue;
 62                     }else {
 63                         if (srcStr.length()/8<1){
 64                             StringBuilder stringBuilder3 = new StringBuilder();
 65                             stringBuilder3.append(srcStr);
 66                             for (int j = 0; j < 8-srcStr.length(); j++) {
 67                                 stringBuilder3.append("0");
 68                             }
 69                             stringBuilder.delete(stringBuilder.toString().lastIndexOf(" "),stringBuilder.toString().length());
 70                             stringBuilder.append(" ");
 71                             stringBuilder.append(stringBuilder3.toString());
 72                             break;
 73                         }
 74                     }
 75                 }
 76             }else {
 77                 if (stringBuilder.toString().length()%8==0){
 78                     String substring = srcStrCopy.substring(count*8);
 79                     srcStr=substring;
 80                     i=-1;
 81                     stringBuilder.append(" ");
 82                     count++;
 83                     flag=true;
 84                     continue;
 85                 }else if (srcStrCopy.length()/8<1){
 86                     StringBuilder stringBuilder2 = new StringBuilder();
 87                     stringBuilder2.append(srcStrCopy);
 88                     for (int j = 0; j < 8-srcStrCopy.length(); j++) {
 89                         stringBuilder2.append("0");
 90                     }
 91                     return stringBuilder2.toString();
 92                 }
 93             }
 94         }
 95         return stringBuilder.toString();
 96     }
 97 
 98     /**
 99      * 获取分割字符串集合
100      * @return
101      */
102     public static List<String> getSpilitStrList(String srcStr){
103         String spilitStr = getSpilitStr(srcStr);
104         String[] splitStrArr = spilitStr.split("\\s");
105         int length = splitStrArr.length;
106         ArrayList<String> stringArrayList = new ArrayList<>();
107         if (length>1){
108             for (int i = 0; i < splitStrArr.length; i++) {
109                 if (i==splitStrArr.length-1){
110                     if (splitStrArr[splitStrArr.length-1].startsWith("0")){
111                         continue;
112                     }
113                 }
114                 stringArrayList.add(splitStrArr[i]);
115             }
116         }else {
117             stringArrayList.add(spilitStr);
118         }
119 
120         return stringArrayList;
121     }
122 
123 }

 

posted @ 2024-07-27 16:18  coderlwz  阅读(21)  评论(0编辑  收藏  举报