0

I have written a program that extracts all the possible sub-strings (contiguous subs) of a given strings:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Empty
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();

            for (int i = 1; i <= str.Length; i++)
            {
                for (int j = 0; j <= (str.Length - i); j++)
                {
                    string subStr = str.Substring(j, i);
                    Console.WriteLine(subStr);
                }
            }

            Console.ReadLine();
        }
    }
}

So, when I enter abc, it returns:

a
b
c
ab
bc
abc

Now, I would like to use this idea to apply it to a list of integers, so that it returns all possible concatenations of the elements of this list of numbers.

For example, if I enter [2 11 8], I would like it to return:

2
11
8
211
118
2118

Is there a method similar to Substring that I can use on a list of numbers to achieve my goal? In other words, can I re-use and modify my little program that works on a string, so it also works on a list of integers?

  • 4
    Not that I'm aware of. I'd just `.ToString()` the lot and use the same code you did for strings. – itsme86 Aug 29 '16 at 17:23
  • 1
    If your string logic instead worked with the input as a character array you could adapt that easily for any array type – stephen.vakil Aug 29 '16 at 17:23
  • 1
    If you take `"abs"` and instead look at it as `["a", "b", "c"]`, then you just need to do `[2, 11, 8].Select(x => x.ToString())` and process the same, but making sure to consider strings of length > 1. – crashmstr Aug 29 '16 at 17:24
  • @Jonathan Wood I wrote it to extract continuous sub-strings, so `ac` does not qualify! –  Aug 29 '16 at 17:26
  • Possible duplicate of [All Possible Combinations of a list of Values](http://stackoverflow.com/questions/7802822/all-possible-combinations-of-a-list-of-values) – Roman Marusyk Aug 29 '16 at 17:26

2 Answers2

2

The approach you are taking seems just fine. You just need to allow it to work with integers (converted to strings).

You could also make your code more generic for any type.

void Main()
{
    Console.WriteLine(GetAllSubstrings('a', 'b', 'c')); 
    Console.WriteLine(GetAllSubstrings(2, 11, 8));  
}

// Define other methods and classes here
public IEnumerable<string> GetAllSubstrings(params object[] args)
{
    for (int i = 1; i <= args.Length; i++)
    {
        for (int j = 0; j <= (args.Length - i); j++)
        {
            yield return string.Join(string.Empty, args.Skip(j).Take(i));
        }
    }
}
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1

Is there a method similar to Substring that I can use on a list of numbers

Yes, there is a direct equivalent List<T> method called GetRange. You can see that by looking at the method signatures:

string: public string Substring(int startIndex, int length)

and

List<T>: public List<T> GetRange(int index, int count)

So, having List<int> list, replace str.Length with list.Count, str.Substring(j, i) with string.Concat(list.GetRange(i, j)) and you are done.

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343