gpt4 book ai didi

c# - 在 C# 中拆分没有分隔符的字符串

转载 作者:行者123 更新时间:2023-11-30 21:30:01 25 4
gpt4 key购买 nike

首先,大家好。

请原谅我糟糕的英语。

我有一个问题,我想制作我的第一个程序,这个程序应该有以下任务:

你有这个 Hashsum 的例子:

9f73c507603e62c48926eb37f0f19f46

程序应将其转换为:

"9","c","8","e","d","1","5","d","0","b","e","b""e","5","c","1","2","6","f","2","3","3","9","5","b","f","8","0","4","8","d","8"

我不会不自己搜索和尝试就问,但请记住我还是一个初学者,我的“解决方案”是这样的:

namespace Hash_Checker
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Hashsum.");
string myhash = Console.ReadLine();
string[] words = myhash.Split(' ');
Console.WriteLine("Modified Hashsum:");
foreach (var word in words)
{

System.Console.Write($"\"{word}\", ");
}
Console.ReadKey();
}
}
}

忽略可能不好的形式,我稍后会尝试写得更好。

我的两个问题是:

您必须像这样输入哈希值:

9 f 7 3 c 5 0 7 6 0 3 e 6 2 c 4 8 9 2 6 e b 3 7 f 0 f 1 9 f 4 6

有分隔符,但我希望你可以不用它来输入它,像这样:

9f73c507603e62c48926eb37f0f19f46

但是当你这样做时,它会以这个输出结束:

"9f73c507603e62c48926eb37f0f19f46",

第二个问题:

我的程序在最后一个数字后添加了一个“,”,但我不希望这样。

示例:

应该是:

"9","c","8","e","d","1","5","d","0","b","e","b""e","5","c","1","2","6","f","2","3","3","9","5","b","f","8","0","4","8","d","8"

但它是:

“9”、“c”、“8”、“e”、“d”、“1”、“5”、“d”、“0”、“b”、“e”、“b” , "e", "5", "c", "1", "2", "6", "f", "2", "3", "3", "9", "5", "b", "f", "8", "0", "4", "8", "d", "8",

最后一个 , 应该被删除。

有人给我提示或解决方案吗?还是对初学者来说有点难?

感谢您的所有帮助!

最佳答案

完成此操作的(许多)方法之一是从输入中读取字符串,从字符串中选择每个字符并使用 LINQ 用引号将其括起来,然后将每个字符串与 string 连接起来。 Join()如下:

namespace Hash_Checker
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Hashsum.");
var input = Console.ReadLine();

//select each character from the string and turn
//each into a string that looks like "<character here>"
var characters = input.Select(x => string.Format("\"{0}\"", x));

//place comma in between each string containing "<character here>"
var formattedString = string.Join(",", characters);

System.Console.Write(formattedString);
Console.ReadKey();
}
}
}

关于c# - 在 C# 中拆分没有分隔符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54893327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com