How to encode for a checkbox to get the result of the image below

I am working on a C # application and I want to accomplish the following task:

I have 12 checkboxes for 12 items, and the user can check all the checkboxes. if the checkboxes 3,4,5,6,8,10,11,12 are checked, I would like to display the following result.

You have selected items 3-6,8,10-12.

Rules:

When the number of consecutive group numbers is 3 or more than 3, a “Show” grouping, for example 3-6

else show individual number. eight

+4
source share
5 answers

First, I suggest you add the value of the entire checkbox in the row, as shown in the figure.

Function call:

string data = "3,5,6,7,8,10,12"; string res = GetResultString(data); 

Functions:

  string GetResultString(string data) { string[] arrData = data.Split(',').ToArray(); List<int> lstData = new List<int>(); foreach (string item in arrData) { lstData.Add(Convert.ToInt16(item)); } lstData.Sort(); string finalStr = string.Empty; if (lstData.Count > 0) { int start = lstData[0]; int end = start; finalStr = string.Empty; for (int index = 1; index < lstData.Count; index++) { if (end + 1 == lstData[index]) { end = lstData[index]; } else { finalStr += appendResult(start, end); start = -1; } if (start == -1) { start = lstData[index]; end = lstData[index]; } } finalStr += appendResult(start, end); } finalStr = finalStr.Trim(','); return finalStr; } string appendResult(int start,int end) { string res = string.Empty; if (end - start > 1) { res += start + "-" + end.ToString() + ","; start = -1; } else { while (start <= end) { res += start.ToString() + ","; start++; } } return res; } 

Hope it works

+1
source

I assume that you made your checkbox with an array (new ...), if you do not do this maunally ...

 int min=13; int max=0; string s = ""; for (int i = 0; i<12; i++) { if (cb[i].checked && i<min) min = i; else if (cb[i].checked == false) if (min != 13) { max = i-1; s = s + min.ToString() + "-" + max.ToString() + " "; min = 13; } } if (cb[11].checked) s = s + min.ToString() + "-12"; // for case the last one is checked // s contains your data 

(I did not check, but I think it should be something like this.)

0
source

try this .. it will work, I tested it

I did not create the checkboxes, so you need to check which checkbox is selected, get the string as from the selected checkboxes 3,4,5,6,8,10,11,12

  string str1 = "3,4,5,6,8,10,11,12"; string[] strArr = str1.Split(','); List<string> strFinal = new List<string>(); int[] myInts = Array.ConvertAll(strArr, s => int.Parse(s)); int arrLn = myInts.Length; Array.Sort(myInts); int intPrevVal = myInts[0]; int intPrevDiff = myInts[0]; int seriesCount = 1; strFinal.Add(Convert.ToString(myInts[0])); for (int j = 1; j < arrLn; j++) { int intCurr = myInts[j]; if (intCurr - intPrevVal == 1) { seriesCount++; } else { if (seriesCount >= 3) { strFinal[strFinal.Count - 1] = strFinal[strFinal.Count - 1] + "-" + intPrevVal; seriesCount = 1; } else if (seriesCount == 2) { strFinal.Add(Convert.ToString(myInts[j - 1])); seriesCount = 1; //strFinal.Add(Convert.ToString(myInts[j])); } strFinal.Add(Convert.ToString(myInts[j])); } intPrevVal = intCurr; } if (seriesCount >= 3) { strFinal[strFinal.Count - 1] = strFinal[strFinal.Count - 1] + "-" + myInts[arrLn - 1]; } else if (seriesCount == 2) { strFinal.Add(Convert.ToString(myInts[arrLn - 1])); } string FinalAns = string.Join(",", strFinal.ToArray()); Response.Write(FinalAns); 
0
source

try it

  var data = new List<int> { 3, 4, 5, 6, 8, 10, 11, 12 }; // data.Sort(); var groups = new List<string>(); var startIndex = 0; for (var i = 1; i < data.Count; i++) { if (data[i - 1] == data[i] - 1) { continue; } groups.Add(startIndex == i - 1 ? data[startIndex].ToString() : data[startIndex] + "-" + data[i - 1] ); startIndex = i; } groups.Add(startIndex == data.Count - 1 ? data[startIndex].ToString() : data[startIndex] + "-" + data[data.Count - 1]); var result = string.Join(",", groups); 

version 2

  [Fact] public void Test() { var data = new List<int> { 3, 4, 5, 7, 8, 10, 11, 12 }; // data.Sort(); var groups = new List<string>(); var startIndex = 0; for (var i = 1; i < data.Count; i++) { if (data[i - 1] == data[i] - 1) { continue; } AddToGroups(groups, startIndex, i, data); startIndex = i; } AddToGroups(groups, startIndex, data.Count, data); var result = string.Join(",", groups); Assert.Equal("3-5,7,8,10-12", result); } private static void AddToGroups(List<string> groups, int startIndex, int actualIndex, List<int> data) { switch (actualIndex - startIndex) { case 1: groups.Add(data[startIndex].ToString()); break; case 2: groups.Add(data[startIndex].ToString()); groups.Add(data[startIndex + 1].ToString()); break; default: groups.Add(data[startIndex] + "-" + data[actualIndex - 1]); break; } } 
0
source

You may have a solution, but all of the above solutions use a string to add data. You can use StringBuilder to optimize performance.

 List<int> selectedCB = new List<int>() { 3, 4, 6, 7, 8, 9, 11, 12 }; string output = GetFormattedOutput(selectedCB); 

Code for formatting data.

 private string GetFormattedOutput(List<int> selectedCB) { // Can be changed if you want to increase // groupby range int rangeBy = 3; int diffBy = 1; int prevValue = 0; List<int> tempList = new List<int>(); StringBuilder formattedOutput = new StringBuilder(); foreach (int currentValue in selectedCB) { var diff = currentValue - prevValue; if(tempList.Count != 0 && diff > diffBy) { // Add the value in templist to formatted output // If three are more numbers are in range // Add the first and last if (tempList.Count >= rangeBy) { formattedOutput.Append(tempList[0].ToString() + "-" + tempList[tempList.Count - 1].ToString()+","); } else { AppendText(formattedOutput, tempList); } tempList.Clear(); } tempList.Add(currentValue); prevValue = currentValue; } if (tempList.Count != 0) { AppendText(formattedOutput, tempList); } formattedOutput.Remove(formattedOutput.Length - 1, 1); return formattedOutput.ToString(); } // To append the numbers in the list string AppendText(StringBuilder output, List<int> tempList) { foreach (var temp in tempList) { output.Append(temp.ToString() + ","); } return output.ToString(); } 
0
source

Source: https://habr.com/ru/post/1501821/


All Articles