111,128
社区成员
发帖
与我相关
我的任务
分享
不好意思小白不懂string txt = "123456789";
StringBuilder tmp = new StringBuilder();
for (var i = 0; i < txt.Length; i += 2)
{
tmp.Append(txt[i]);
if (i < txt.Length - 1)
{
tmp.Append(txt[i + 1]);
tmp.AppendLine();
}
}
Console.WriteLine(tmp.ToString().Trim());
Console.WriteLine(Regex.Replace(txt, @"[\s\S]{2}", "$0\r\n").Trim());不过。。。什么两个代码都不行啊,输出的文件还是一行。。。

话说这个论坛的验证码真难认。。。string ss = Regex.Replace(s, @"(\w\w)", "$1\n");string s=File.ReadAllText("1.txt");
//string s = "123456789";
string ss = "";
for (int i = 0; i < s.Length; i++)
{
ss += s[i];
if ((i + 1) % 2 == 0)
ss += '\n';
}
File.WriteAllText("2.txt",ss);
//读取到结果
string str = "123456789";
//处理后字符串数组的长度
int length;
//判断取出长度是不是2的整数倍
if (str.Length % 2 != 0)
{
length = str.Length / 2 + 1;
}
else
{
length = str.Length / 2;
}
string[] result = new string[length];
for (int i = 0; i < length; i++)
{
if (i == length - 1)
{
result[i] = str.Substring(i * 2, 1);
}
else
{
result[i] = str.Substring(i * 2, 2);
}
}