1, 2, 3, 5, 7, 8, 10, 11, 12, 13, 14, 15, 16, 21, 22-"1 ~ 3, 5, 7 ~ 8, 10 ~ 16,21 ~ 22

Source: Internet
Author: User

In order to omit the space and make it visible to the operator who manually fills in the paper, the volume number on the card of the materials shelf is determined to be classified and sorted, as shown in

A-3, A-4, A-5, A-8 forming A-3 ~ 5, 8, etc.

The following code uses a few auxiliary list <int>:

/// <Summary>
/// Similar to 1, 2, 3, 5, 7, 8, 10, 11, 12, 13, 14, 15, 16, 21, 22, convert to 1 ~ 3, 5, 7 ~ 8, 10 ~ 16,21 ~ 22
/// </Summary>
/// <Param name = "strinput"> input string, for example, 1, 2, 3, 5, 7, 8, 10, 11, 12, 13, 14, 15, 16, 21, 22 </param>
/// <Returns> the new string, for example, 1 ~ 3, 5, 7 ~ 8, 10 ~ 16,21 ~ 22 </returns>
Private string makesimplestring (string strinput)
{
String stroutput = "";
// Put the integer in Il
String [] Ss = strinput. Split (',');
List <int> IL = new list <int> ();
For (INT I = 0; I <ss. length; ++ I)
{
Try
{
Int K = convert. toint32 (ss [I]);
Il. Add (k );
}
Catch
{
If (stroutput = "")
Stroutput = ss [I];
Else
Stroutput + = "," + SS [I];
}
}


If (IL. Count> 0)
{
// Place the continuous numbers in IL into different lists <int>.
Il. Sort ();
List <list <int> ill = new list <int> ();
Int Prev = il [0];
List <int> ltemp = new list <int> ();
Ltemp. Add (prev );
Ill. Add (ltemp );
For (INT I = 1; I <Il. Count; ++ I)
{
If (IL [I]-Prev = 1)
{
Ill [ill. Count-1]. Add (IL [I]);
}
Else
{
Ltemp = new list <int> ();
Ltemp. Add (IL [I]);
Ill. Add (ltemp );
}
Prev = il [I];
}

// Generate the result string
For (INT I = 0; I <ill. Count; ++ I)
{
If (ill [I]. Count = 1)
{
If (stroutput = "")
Stroutput = ill [I] [0]. tostring ();
Else
Stroutput + = "," + ill [I] [0]. tostring ();
}
Else
{
If (stroutput = "")
Stroutput = ill [I] [0]. tostring () + "~ "+ Ill [I] [ill [I]. Count-1]. tostring ();
Else
Stroutput + = "," + ill [I] [0]. tostring () + "~ "+ Ill [I] [ill [I]. Count-1]. tostring ();
}
}
}

Return stroutput;
}

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.