读txt文件,怎么实现生成两个字符就回车的txt文件?

null508 2016-07-12 05:13:24
比如一个txt内容为:123456789
想生成:
12
34
56
78
9
这样的txt
...全文
210 12 打赏 收藏 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
null508 2016-07-13
  • 打赏
  • 举报
回复
引用 11 楼 yuankaiwsl 的回复:
见笑,把'\n'都改成Environment.NewLine或者'\r\n'
搞定,谢谢
巴士上的邂逅 2016-07-13
  • 打赏
  • 举报
回复
见笑,把'\n'都改成Environment.NewLine或者'\r\n'
null508 2016-07-13
  • 打赏
  • 举报
回复
引用 8 楼 starfd 的回复:
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());
版主这个好像可以,但怎么输出成一个txt文件啊?我加了一句File.WriteAllText("d:\\tt\\222.txt", txt);还是原来的格式 不好意思小白不懂
null508 2016-07-13
  • 打赏
  • 举报
回复
引用 7 楼 yuankaiwsl 的回复:
引用 6 楼 qq_15632417 的回复:
不过。。。什么两个代码都不行啊,输出的文件还是一行。。。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //读取到结果 string s = "1234567890"; string ss = Regex.Replace(s, @"(\w\w)", "$1\n"); File.WriteAllText("d:\\tt\\222.txt", ss); } } } 输出的还是一行
  • 打赏
  • 举报
回复
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());
巴士上的邂逅 2016-07-13
  • 打赏
  • 举报
回复
引用 6 楼 qq_15632417 的回复:
不过。。。什么两个代码都不行啊,输出的文件还是一行。。。
null508 2016-07-13
  • 打赏
  • 举报
回复
不过。。。什么两个代码都不行啊,输出的文件还是一行。。。
null508 2016-07-13
  • 打赏
  • 举报
回复
谢谢各位大神 话说这个论坛的验证码真难认。。。
巴士上的邂逅 2016-07-12
  • 打赏
  • 举报
回复
对,正则更简单
string ss = Regex.Replace(s, @"(\w\w)", "$1\n");
Poopaye 2016-07-12
  • 打赏
  • 举报
回复
不如找个带正则的文本编辑器 查找:(\w\w) 替换:\1\n
巴士上的邂逅 2016-07-12
  • 打赏
  • 举报
回复
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);
带头大哥_ 2016-07-12
  • 打赏
  • 举报
回复
//读取到结果
            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);
                }
            }

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧