0

I have a multiline textbox that contains 10 digit mobile numbers separated by comma. I need to achieve string in group of at least 100 mobile numbers.

100 mobile numbers will be separated by 99 comma in total. What i am trying to code is to split the strings containing commas less than 100

public static IEnumerable<string> SplitByLength(this string str, int maxLength) 
  {
    for (int index = 0; index < str.Length; index += maxLength) {
    yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
  }
}

By using above code, I can achieve 100 numbers as 100 numbers will have 10*100(for mobile number)+99(for comma) text length. But the problem here is user may enter wrong mobile number like 9 digits or even 11 digits.

Can anyone guide me on how can I achieve this. Thank you in advance.

9
  • 4
    Thus your problem is not the splitting but the validation of the inputs? Commented May 31, 2017 at 9:57
  • "user may enter wrong mobile number" but of course your user will always use the comma correctly? Seems you need to validate your input before further processing. Commented May 31, 2017 at 9:59
  • 1
    Don't use such a field. If anything, it's very hard for a user to enter the data correctly. A quick&dirty solution would be to use a multiline textbox. A newline is a far, far, far better separator for humans than a comma. Even better, use an editable combobox that displays all items. You can validate modified items individually. You only need an Add button to add new items. Commented May 31, 2017 at 10:02
  • You should check if the value entered is correct, maybe use some regular expressions. If you want to check only 1 phone number, you could do something like ([0-9]{10}) or something or better depending on the phone format you want to have. If you want to check if the string (with the commas) is valid, then do ([0-9]{10},){100} or ([0-9]{10},)* depending on wether you always want 100 phone numbers or at least one Commented May 31, 2017 at 10:03
  • comma will be added automatically after 10 digits but the problem is that it also supports excel upload that may have wrong digit of number Commented May 31, 2017 at 10:03

3 Answers 3

3

You could use this extension method to put them into max-100 number groups:

public static IEnumerable<string[]> SplitByLength(this string str, string[] splitBy, StringSplitOptions options, int maxLength = int.MaxValue)
{
    var allTokens = str.Split(splitBy, options);
    for (int index = 0; index < allTokens.Length; index += maxLength)
    {
        int length = Math.Min(maxLength, allTokens.Length - index);
        string[] part = new string[length];
        Array.Copy(allTokens, index, part, 0, length);
        yield return part;
    }
}

Sample:

string text = string.Join(",", Enumerable.Range(0, 1111).Select(i => "123456789"));
var phoneNumbersIn100Groups = text.SplitByLength(new[] { "," }, StringSplitOptions.None, 100);
foreach (string[] part in phoneNumbersIn100Groups)
{
    Assert.IsTrue(part.Length <= 100);
    Console.WriteLine(String.Join("|", part));
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have a few options,

  1. Put some kind of mask on the input data to prevent the user entering invalid data. In your UI you could then flag the error and prompt the user to reenter correct information. If you go down this route then something like this string[] nums = numbers.Split(','); will be fine.
  2. Alternatively, you could use regex.split or regex.match and match on the pattern. Something like this should work assuming your numbers are in a string with a leading comma or space

    Regex regex = new Regex("(\s|,)\d{10},)";

    string[] nums = regex.Split(numbers);

Comments

0

This be can resolved with a simple Linq code

public static IEnumerable<string> SplitByLength(this string input, int groupSize)
{
    // First split the input to the comma.
    // this will give us an array of all single numbers
    string[] numbers = input.Split(',');

    // Now loop over this array in groupSize blocks
    for (int index = 0; index < numbers.Length; index+=groupSize)
    {
        // Skip numbers from the starting position and 
        // take the following groupSize numbers, 
        // join them in a string comma separated and return 
        yield return string.Join(",", numbers.Skip(index).Take(groupSize));
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.