首页 > 解决方案 > 如何将用户输入的字符串转换为输入中单个字母的数组?

问题描述

我正在制作一个解决方案,用户输入一个单词或只输入字母,它会显示单词的排列而不重复。我怎么能把它输入到一个字符数组中。(我正在修改另一个人的代码来尝试这样做)所以我想分配一个给定的值,我在下面的代码中评论了 SOMEHOW 以便我可以拆分它进入数组。我是编码新手,希望能得到帮助。

public static void Main()
    {
                string input;
        //char word_inputed;
        Console.WriteLine("Please print a word");
        input = Console.ReadLine();
        //string phrase = "The quick brown fox jumps over the lazy dog.";
        string[] words = input.Split(' ');

        foreach (var word in words)
        {

            foreach (var letter in word)
            {

               //SOMEHOW values = Console.Write($"{letter},");

            }
        }

        ArrayList list = new ArrayList();
        list.AddRange(values.Split(new char[] { ',' }));

        char[] word_inputed = { 'A' };
        int n = word_inputed.Length;
        findPermutations(word_inputed, 0, n);



    }

标签: c#arraysstringsplitpermutation

解决方案


这是微软示例

// Sample for String.ToCharArray(Int32, Int32)
using System;

class Sample {
    public static void Main() {
    string str = "012wxyz789";
    char[] arr;

    arr = str.ToCharArray(3, 4);
    Console.Write("The letters in '{0}' are: '", str);
    Console.Write(arr);
    Console.WriteLine("'");
    Console.WriteLine("Each letter in '{0}' is:", str);
    foreach (char c in arr)
        Console.WriteLine(c);
    }
}
/*
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z
*/


推荐阅读