茶叶蛋爱好者 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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器