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