An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
@MiPakTeh , based your description, you want to generate all the length-3, length-2, length-1 combinations of integers or string from text file.
I make a code example and you could refer to it.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Data1.AddRange(File.ReadAllLines(@"Tool.txt"));
}
List<string> Data1 = new List<string>();
char[] b;
string newstring;
private void button1_Click(object sender, EventArgs e)
{
for (int ii = 0; ii < Data1.Count; ii++)
{
List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
var arr = Data1[ii].Split(',');
foreach (var item in arr)
{
IEnumerable<IEnumerable<char>> result =
GetPermutations(item.ToCharArray().AsEnumerable(), 2);
foreach (var item1 in result)
{
b = item1.ToList().ToArray();
Array.Sort(b);
newstring = new string(b);
if(!newstring.StartsWith("0"))
{
list1.Add(newstring);
}
//list1.Add(newstring);
}
IEnumerable<IEnumerable<char>> result1 =
GetPermutations(item.ToCharArray().AsEnumerable(), 3);
foreach (var item1 in result1)
{
b = item1.ToList().ToArray();
Array.Sort(b);
newstring = new string(b);
if (!newstring.StartsWith("0"))
{
list2.Add(newstring);
}
else
{
newstring=newstring.Remove(0, 1);
list1.Add(newstring);
}
}
}
listBox1.Items.Add(Data1[ii]);
listBox1.Items.Add("number 2");
listBox1.Items.AddRange(list1.ToArray());
listBox1.Items.Add("number 3");
listBox1.Items.AddRange(list2.ToArray());
}
}
static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutations(list, length - 1)
.SelectMany(t => list.Where(e => !t.Contains(e)),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
}
Result:
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.