本文介绍了从.TXT文件中检索字符串,然后在C#中解析它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是编程的新手,并且正在学习视频课程以学习C#.

我需要一点帮助来进行此练习:

Hello everybody,

I''m quite new to programming, and I''m following a video-course in order to learn C#.

I need a little help to do this exercise:

  1. 使用string打开 .TXT 文件,如下所示:
  1. Open a .TXT file with strings like this:
+01/01/1918 11:59:59-1-2-3+


  • 检索string(我以为一个),并获取以"-"定界符开头的数字
    (在这种情况下,数字为1 2 3)
  • 求值每个数字(必须为0lt; n>< 10)>
  • 如果数字超出范围,请显示一条消息(控制台)并处理下一个string

  • Retrieve the strings (one by one I presume) and get the numbers preceded by the "-" delimiter
    (in this case, the numbers: 1 2 3)
  • Evaluate every number (must be 0<n><10)>
  • If a number is out of range, show a message (console) and process the next string


  • 谢谢您的帮助.



    Thank you for your help.

    推荐答案


    static void Main(string[] args)
    {
        string text = "+01/01/1918 11:59:59-1-2-3+";
        string[] words = text.Split(''-'');
        List<string >buffer=new List<string>();
        //Initial vords
        foreach (var word in words)
        {
            Console.WriteLine(word);
            buffer.Add(word);
        }
        buffer.Remove(buffer[0]);//remove first words
        buffer[2] = buffer[2].Remove(buffer[2].Length - 1, 1);//remove simbol +
        //Show final list with result : 1, 2 , 3
        foreach (var aux in buffer)
        {
            Console.WriteLine(aux);
        }
        Console.ReadLine();
    }



    这篇关于从.TXT文件中检索字符串,然后在C#中解析它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-26 12:36