如何从字符串中取出两个单词然后添加 -

how to take two words from a string and then add a -

各位早安 所以我有这个 sanario 用户在文本框中输入文本 文本格式如下

0123456789ABCDEF

我希望能够将用户给出的文本拆分为

01-23-45-67-89-AB-CD-EF

这可能吗?

然后,如果可能的话,我怎样才能将这个字符串转换为字节?

全部在 c# 中完成

提前致谢

string output = "0123456789ABCDEF";
int i = 2;

while (i < output.Length) {
    output = output.Insert(i, "-");
    i += 3;
}

你能为字节数组这样做吗?

string output = "0123456789ABCDEF";
int i = 2;

while (i < output.Length) {
    output = output.Insert(i, ",");
    i += 3;
}

byte[] array = {output};

这里可能已经回答了:Converting String to Byte Array

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

这是我的首选方式:

var result = String.Join("-", text.Buffer(2).Select(x => new string(x.ToArray())));

您只需要从 Microsoft 的 Reactive Framework 团队获取 Interactive Framework (NuGet Ix-Main)。