Recently, a bar code printing program requires that the number of box numbers increase based on the number of printed items. The box number format is string tail number.
Analysis:
- Separate non-numeric characters and numeric characters according to the box number format;
- Second, convert the numeric character to the numeric character type for calculation. The length of the numeric character is recorded before conversion;
- The length of the number after the auto-increment is compared with that of the original number type. when the length of the number after the auto-increment is insufficient, the first number is filled with 0;
- Finally, the original part of the non-numeric character and the auto-incrementing part of the numeric character are spliced to obtain the result.
Defects:
- The length of numeric characters is limited, and conversion from string to numeric type is more prone to errors;
The specific implementation code is as follows:
1 private void btncalc_click (Object sender, eventargs E) 2 {3 string input = txtstr. text. trim (); 4 int addxs = convert. toint32 (txtxs. text. trim (); 5 6 // number Character index 7 int strartno = 0; 8 // fixed character Part 9 string gdzf = string. empty; 10 // variable column character section 11 string kbsz = string. empty; 12 13 for (INT I = 0; I <input. length; I ++) 14 {15 // determines whether the digit type is 16 if (58> input [I] & input [I]> 47) 17 {18 strartno = I; 19 break; 20} 21} 22 23 // fixed column assignment 24 gdzf = input. substring (0, strartno); 25 // variable column value 26 kbsz = input. substring (strartno, input. length-strartno); 27 28 // variable Column Length 29 int KBCD = kbsz. length; 30 31 int chsz = 0; 32 try33 {34 chsz = convert. toint32 (kbsz); 35} 36 catch (exception) 37 {38 txtresult. TEXT = "format error"; 39} 40 41 chsz = chsz + addxs; 42 43 // compare the length of original numeric characters with the length of Self-incrementing characters 44 If (chsz. tostring (). length <KBCD) 45 {46 // Number of zeros of 47 int BLGs = KBCD-chsz. tostring (). length; 48 string zero = string. empty; 49 for (INT I = 0; I <BLGs; I ++) 50 {51 zero = zero + "0"; 52} 53 54 kbsz = zero + chsz; 55} 56 else57 {58 kbsz = chsz. tostring (); 59} 60 61 txtresult. TEXT = gdzf + kbsz; 62}
The test results are as follows:
Summary:
- The above is a personal algorithm. Due to some restrictions, if there is a better algorithm, I hope you can give some advice.
String tail number auto-Increment