茶叶蛋爱好者 2018-05-10 04:50 采纳率: 0%
浏览 1442
已采纳

读取TXT文件后,将内容存入到数组中,换行符被当作0存入,怎么去掉?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Collections;
using System.IO;

namespace ccs_getchar
{
class Program
{
static void Main(string[] args)
{
for (int n = 0; n < 6; n++)
{
Console.WriteLine();
Console.WriteLine("please input the chinese character:");
string chinese = Console.ReadLine();
string path = @"c:\asd\gbcdbig.txt";
string[] strNew;

            int cctarget = 0;
            ArrayList cc = new ArrayList();
            string[] contents = File.ReadAllLines(path, Encoding.Default);
            string flag = Convert.ToString('*');
            for (int i = 0; i < contents.Length; i++)
            {
                strNew = contents[i].Split(new char[1] { ' ' });            //strNew.Indexof
                // Console.WriteLine(contents[i]);

                for (int m = 0; m < strNew.Length; m++)
                {


                    if (strNew[m] == chinese)
                    {
                        if (cctarget == 0)
                        {
                            for (int j = 0; j < strNew.Length; j++)
                            {
                                cc.Add(strNew[j]);

                            }
                            cctarget = cctarget + 1;
                            break;
                        }
                    }
                    else if (cctarget == 1)
                    {
                        if (strNew[m] != flag)
                        {
                            for (int j = 0; j < strNew.Length; j++)
                            {
                                cc.Add(strNew[j]);


                            }

                        }
                        else
                            cctarget = cctarget + 1;
                        break;
                    }
                    else if (cctarget == 2)
                        break;

                }

            }
            cc.RemoveRange(0, 4);
            string[] rd_pos = (string[])cc.ToArray(typeof(string));
            int[] pos_int = new int[rd_pos.Length];
            for (int i = 0; i < rd_pos.Length; i++)
            {
                int.TryParse(rd_pos[i], out pos_int[i]);

            }
            foreach (var item in pos_int)
            {
                Console.Write(item + " ");
            }

            Console.ReadKey();
        }
    }

    }
}

TXT的文件格式如下
图片说明


  • 写回答

6条回答 默认 最新

  • Go 旅城通票 2018-05-10 05:51
    关注

    ReadAllLines不会读取换行的,而且注意你的存储编码如果是utf-8的,ReadAllLines要制传递utf-8,要不会乱码

    而且你的数组指的那个

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵