String tail number auto-Increment

Source: Internet
Author: User

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:

  1. Separate non-numeric characters and numeric characters according to the box number format;
  2. Second, convert the numeric character to the numeric character type for calculation. The length of the numeric character is recorded before conversion;
  3. 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;
  4. 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:

  1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.