Source:
Recently, the company needs to make SMS transfer service platform. Because it is the first time to contact this business, there are many hops in the middle. Some of them are: upstream channels need to configure information templates, provide variable information in the interface, downstream channels can only provide complete information, unable to provide variable information (Tucao: too LAN, do not want to make it), there is no way. , we have to extract the variable information by ourselves, and then submit the variable information to the upstream channel in a fixed format. The variable information is collectively referred to as variable information.
After efforts, a test class has been completed. Variable information in the content can be automatically extracted according to the complete information content and template, and the template can be automatically divided into two or more pieces according to the length of the complete information, as well as variable information corresponding to the number of pieces. If you don't say much, go to the code directly:
package com.hyfd.service; import java.util.ArrayList; import java.util.List; public class ArgsGet { // The length of the information segmentation condition (set to 100 characters in this case) private static final int STR_SPLIT_MAX_LENGTH = 100; public static void main(String[] args) { String smsStr = ToDBC("The cavalry of Genghis Khan,***At the same speed as the armored forces of the 20th century;The bed crossbow of the Northern Song Dynasty" + "1500 meters and sniper in the 20th century×××Almost;But these are still ancient cavalry and×××It's just not possible" + "Modern forces are competing. The basic theory decides everything, which is clearly seen by the future historiography school. And you are reflected" + "Low tech blindfolded. You lie in the hotbed of modern civilization and enjoy the ultimate decision of human destiny" + "There was no mental preparation for the final battle."); getArgsList(smsStr); } /** * <h5>Function: obtain the variable information after information splitting < / H5 > * @param smsStr Complete information content */ private static List<String> getArgsList(String smsStr) { String ptTemplateAll = "#*#Cavalry,***Speed and#*#The armored forces of the century are comparable;#*#Of#*#,Shooting range#*#Rice, " + "And#*#Century#*#Almost;But these are still ancient#*#And#*#Nothing more,Impossible#*#Power struggle.#*#Decide " + "Fix everything,#*#This is clearly seen. And you,But it's reflected#*#Blindfolded. You lie down#*#Hotbed " + "Zhong An Yu#*#,For the coming#*#No mental preparation at all. "; String[] ptTemplate = ptTemplateAll.split("#\\*#"); System.out.println("Complete information[" + smsStr + "]"); System.err.println("Complete template[" + ptTemplateAll + "]"); // Split information content List<String> smsList = smsSplit(smsStr, ptTemplate); // Split template based on information content List<List<String>> templateSplit = templateSplist(smsList, ptTemplate); for (int i = 0; i < templateSplit.size(); i++) { System.out.println("The first" + (i + 1) + "Segment split template" + templateSplit.get(i) + ""); } List<String> argsList = new ArrayList<String>(); // Obtain the corresponding variable information according to the split information content and template for (int i = 0; i < smsList.size(); i++) { String tempSmsStr = smsList.get(i); List<String> templateSplist = templateSplit.get(i); for (String template : templateSplist) { if (!"".equals(template)) { tempSmsStr = tempSmsStr.replaceFirst(template, ","); } } if (tempSmsStr.endsWith(",")) { tempSmsStr = tempSmsStr.substring(0, tempSmsStr.length() - 1); } argsList.add(tempSmsStr); System.err.println("The first" + (i + 1) + "Segment variable information[" + tempSmsStr + "]"); } return argsList; } /** * <h5>Function: split information content < / H5 > * @param smsStr Complete information content * @param ptTemplate Array template information * @return */ private static List<String> smsSplit(String smsStr, String[] ptTemplate){ List<String> smsList = new ArrayList<String>(); StringBuffer sbf = new StringBuffer(); for (String template: ptTemplate) { if (!"".equals(template)) { // Once the length of information content exceeds 100, split the information if (sbf.length() > STR_SPLIT_MAX_LENGTH) { // System.out.println(sbf.toString()); smsList.add(sbf.toString()); sbf.setLength(0); } sbf.append(smsStr.substring(0, smsStr.indexOf(template) + template.length())); smsStr = smsStr.substring(smsStr.indexOf(template)+template.length()); } } smsList.add(sbf.toString()); return smsList; } /** * <h5>Function: split the template according to the information content < / H5 > * @param smsList Information content after splitting * @param ptTemplate Template information * @return */ private static List<List<String>> templateSplist(List<String> smsList, String[] ptTemplate){ List<List<String>> templateList = new ArrayList<List<String>>(); for (String smsStr : smsList) { // System.out.println(smsStr); // 1. End identification of obtaining information String sbf = ""; for (String template : ptTemplate) { if (!"".equals(template)) { if (smsStr.endsWith(template)) { // System.out.println(template+"="+smsStr.indexOf(template)); sbf = template; } } } // 2. Split the information template according to the information ID List<String> tempList = new ArrayList<String>(); StringBuffer tempSbf = new StringBuffer(); for (int i = 0; i < ptTemplate.length; i++) { tempSbf.append(ptTemplate[i]); if (!"".equals(tempSbf.toString())) { tempList.add(tempSbf.toString()); if (sbf.equals(tempSbf.toString())) { ptTemplate[i] = ""; break; } else { ptTemplate[i] = ""; } } tempSbf.setLength(0); } templateList.add(tempList); // System.out.println(JSONObject.toJSONString(tempList)); // System.out.println("-----------------------------"); } return templateList; } /** * <h5>Function: full angle half angle < / H5 > * This is mainly for the purpose of standardizing the information content. Therefore, the half angle information is processed. When not in use, it can be directly removed from the reference * @author zhangpj @date 2018 November 5th 2013 * @param str * @return */ public static String ToDBC(String str) { char[] c = str.toCharArray(); for (int i = 0; i < c.length; i++) { // 12288 for full space and 32 for half space if (c[i] == 12288) { c[i] = (char) 32; continue; } // The corresponding relationship between half angle (33-126) and full angle (65281-65374) of other characters is: the difference is 65248 if (c[i] > 65280 && c[i] < 65375) c[i] = (char) (c[i] - 65248); } return new String(c); } }
The test results show that the template is segmented and the corresponding variable information is obtained perfectly. The effect is as follows
Epilogue
Due to the lack of optimization in time, if there is something wrong, welcome to give advice, thank you!