c# - 如何将用户输入的字符串转换为输入中单个字母的数组?
问题描述
我正在制作一个解决方案,用户输入一个单词或只输入字母,它会显示单词的排列而不重复。我怎么能把它输入到一个字符数组中。(我正在修改另一个人的代码来尝试这样做)所以我想分配一个给定的值,我在下面的代码中评论了 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);
}
解决方案
这是微软示例
// 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
*/
推荐阅读
- material-ui - Material-UI 中的引导间距断点相当于什么?
- php - 带有 OR 条件的 CakePHP 链自定义查找器
- javascript - AR.JS 对移动设备和角落点击事件的最终答案
- javascript - Redux,“删除对象”在嵌套对象中无法正常工作
- javascript - 我无法单击确认框中的“确认”按钮,cy.eq() 失败,因为此元素已与 DOM 分离。抛出错误
- docker - 无法从 docker compose 连接到静态 VPN
- ag-grid - 如何使用 AG Grid 和 Adaptable/ 在屏幕上显示警报
- near - 在 indexer localnet 附近运行时出错,无法生成 config.json
- gmail - 如何计算 Gmail 搜索中的项目数
- swift - 更改 UISearchBar 的文本字段字体也会更改它的高度