Home > OS >  What could be the best way of extracting substrings of varying length in c#
What could be the best way of extracting substrings of varying length in c#

Time:07-19

I looked up a lot and to be honest I even know how to do this but I want a smarter dynamic method in order to extract substrings of varying lengths from a string. For instance I have these 2 strings as follows 1. ML 49420220831LDHRJP 2. Y6456720220930RJPLDH so these strings should be divided into sub strings like for the first string its substrings should look like this ML, 494 (but this should have a whitespace in the beginning the 494 one), 20220832, LDH and lastly RJP same way for the second string it should be Y6, 4567, 20220930, RJP and lastly LDH.

Any input will be appreciated.

CodePudding user response:

        int[] array = new int[] { 2, 4, 8, 3, 3 };
        List<string> someList = new List<string>();
        int j = 0;

        for (int i = 0; i < array.Length; i   )
        {
            string str = delta.Substring(j, array[i]);
            someList.Add(str);
            j  = str.Length;
        }
  • Related