问题
I need to make a combinaion of Stings "a" "b" "c" "d". I've tried putting them in a list then parsing a foreach methord through them, but to no avail.
What else can I do?
回答1:
I will give you the logic.
Create a List<String>
and then go on adding each string to it.
List<String> s = new List<String>();
s.add("a");
s.add("b");
s.add("c");
s.add("d");
Once you added all the strings, then generate a random number between minimum and maximum index like this :
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
Then, while printing each string in the loop over the List with this number, be sure to check that the same random number is not repeated for the nex iteration.
回答2:
Having the strings in an array you can use the function below to print all permutations so it outputs: abcd, abdc, adbc, etc.
Recursive permutation (from MSDN):
public static void Permute(string[] strings, int start, int finish)
{
if (start == finish)
{
for (int i = 0; i <= finish; ++i)
{
Console.Write(strings[i] + " " );
}
Console.WriteLine("");
}
else
{
for (int i = start; i <= finish; ++i)
{
string temp = strings[start];
strings[start] = strings[i];
strings[i] = temp;
Permute(strings, start+1, finish);
temp = strings[start];
strings[start] = strings[i];
strings[i] = temp;
}
}
} // Permute()
回答3:
Will a list do you?
List<String>myStrings = new List<String>();
myStrings.Add("a");
...
myStrings.Add("d");
You should be able to loop through that
回答4:
if you have a set number of strings you could use
var s = String.Format("{0}{1}{2}{3}", stringA, stringB, stringC, stringD);
otherwise a for/foreach loop would be the way forward,
var sb = new StringBuilder();
var strings = new List<string>();
// Add strings to list
for (var i = 0; i < strings.Count; i++)
{
sb.Append(strings[i]);
}
var s = sb.ToString();
I wouldn't use string + string + string style concatenation as this is bad practice due to the way strings work in memory.
EDIT: I haven't tested the code it was written in the browser! let me know if you have any issues.
Just seen the comments above, the code I have posted will always output the strings in the same order so may not be what you want.
HTH
OneShot
回答5:
List<String>stringList = new List<String>();
stringList.Add("a");
stringList.Add("b");
...
...
foreach(string item in stringList)
{
string text=item;
}
回答6:
This example does all the combinations (proper):
using System;
using System.Linq;
using System.Collections.Generic;
public static class Program
{
public static void Main(string[] args)
{
var list = new [] { "a", "b", "c", "d" };
foreach (var combi in Enumerable.Repeat(list, list.Length).CartesianProduct())
Console.WriteLine(string.Join(" ", combi));
}
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] {item}));
}
}
Output:
a a a a
a a a b
a a a c
a a a d
a a b a
a a b b
a a b c
a a b d
a a c a
a a c b
....
d d b c
d d b d
d d c a
d d c b
d d c c
d d c d
d d d a
d d d b
d d d c
d d d d
If you needed permutations, you can drop-in an algorithm from
- MoreLinq
- CodeProject http://www.codeproject.com/KB/recipes/Combinatorics.aspx
来源:https://stackoverflow.com/questions/6276636/how-to-make-a-combination-of-strings-in-c