Convert the defined string to the target value according to the length of the integer Id value

    Recently, the interface handover system is used in two systems whose functions have been almost realized. The parameters passed from system A are processed by themselves and correspond to the parameters required by system B, so that B can realize the function and finally return the result.

1. Since the two parties did not have much unity in the interface definition, it appeared during the handover. When editing and deleting, the system A sent the integer Id 1 2 3 4 and the system B customized the primary key when adding a specific implementation. Id value for 36-bit guid type: 94f26f5c-ba83-4209-bd07-f5e8b01e11b5

2. At the beginning of the coordination, both parties hoped that the other party would add an ID to correspond to it, but A has already adjusted several companies according to the interface, and B's system is already in use by many companies, and since I only do parameter forwarding, so If I build a database and save the mapping relationship myself, I will lose the meaning of my intermediate system. Finally, I decided to follow the ID given to me by A on B. I will supplement 36 bits and forward it to B for implementation. That is, when B is added, the primary key Id is determined by me. A's ID complements 36-bit generation.


There is so much nonsense, in fact, I want to explain that the interface definition of   the caller, the implementer, and the caller is not uniform, and it kills people! !

The specific implementation is actually very simple. Just define a 36-bit variable of String type, and then intercept this variable according to the length of the Id passed by the A system, and then add the Id of A to get the target value that meets the needs. Mainly The substring method of the String class is used, and then there is replace  replace All . Because I don't know which characters are replaced, the replace series  method does not apply to this requirement.

The code is:

/**
         * Get the complemented 36-bit Id value
         *
         * @param str 36-bit variable
         * @return returns the complemented id type
         */
        public String getComplementId(String str,String targetId){
            String resultId = str. substring (0, str.length()-targetId.length())+targetId;
            if(StringUtils.isEmpty( resultId )){
                return null;
            }else{
                return resultId;
            }
    
        }

                 str is defined to satisfy the B system implementation Id be60d293-907a-4fe4-8fb1-0a61f0fd4 555

                 I convert the integer ID passed by targetId A to String   213

                resultId The final result value be60d293-907a-4fe4-8fb1-0a61f0fd4 213


By the way, replaceAll() replace() of String

The two methods have the same function in the specific implementation. Replace the target string with the specified string.


String guid2 = "areaarea-jied-0000-0000-00000213";


String ada = guid.replaceAll("0000", "1111");
String da = guid.replace("0000", "1111");

output:

areaarea-daqu-
1111-1111-11110213 areaarea-daqu-1111-1111-11110213

only

There is a difference when using escape characters for substitution. The parameter of replaceAll is regex, which is a regular expression. First of all, it will be escaped, of course, it will make an error if it is not handled properly

And replaceAll is also called in the replace method, so replaceAll is better in performance     

so

     When the string cannot determine whether it has an escape character, and it does not need to be escaped, it is recommended to use the replace function    . Otherwise, use the replaceAll function






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325556495&siteId=291194637