资本化的排列

Sco*_*ain 3 .net c# string capitalization

我想建立一个列表,其中包含一个单词大写的每个可能的排列.所以它会

List<string> permutate(string word)
{
    List<string> ret = new List<string>();
    MAGIC HAPPENS HERE
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

所以说我放入"快乐"我应该得到一个阵列

{快乐,快乐,快乐,快乐,haPpy,HaPpy ......哈欠,哈普,快乐,快乐}

我知道有很多函数可以将第一个字母大写,但是如何在单词中做任意字母?

LBu*_*kin 8

如果将字符串转换为char数组,则可以修改单个字符.像这样的东西应该做的伎俩......

public static List<string> Permute( string s )
{
  List<string> listPermutations = new List<string>();

  char[] array = s.ToLower().ToCharArray();
  int iterations = (1 << array.Length) - 1;

  for( int i = 0; i <= iterations; i++ )
  {
    for( int j = 0; j < array.Length; j++ )
    array[j] = (i & (1<<j)) != 0 
                  ? char.ToUpper( array[j] ) 
                  : char.ToLower( array[j] );
    listPermutations.Add( new string( array ) );
  }
  return listPermutations;
}
Run Code Online (Sandbox Code Playgroud)