【问题标题】:get a part of string from multiline/column string从多行/列字符串中获取字符串的一部分
【发布时间】:2017-06-19 13:37:58
【问题描述】:

应用程序.net C# 我有问题如何从 lista.txt 文件中取出(单击按钮)这个(圆形红色)值。事实上,那是一个汇率表。价值观每天都在变化。 我的总是在同一个位置。

到目前为止,使用 line.substring 我已经将整列内联: 5,2599355,3058300,2770031,0057322,4095196,1123050,8419020,7941597,0027418,7470806,9262237,4796281,729514

我如何得到一个粗体部分。它的近端字符串(7,479628)。 非常感谢。

【问题讨论】:

  • 请添加到目前为止编写的代码以达到所需的行

标签: c# arrays string substring


【解决方案1】:

我刚刚从一个人那里收到了可以正常工作的解决方案。他的建议是检查我感兴趣的欧元。所以,下载文件后,方法是:

        int counter = 0;
        string line;

        System.IO.StreamReader file = new System.IO.StreamReader(fullPath);
        while ((line = file.ReadLine()) != null)
        {
            if (counter == 0)//jump first row, it's header don't need it
            {
                counter++;
                continue;
            }
            if (line.Contains("EUR"))
            {
                line = line.Replace(" ", "");//clean spaces
                line = line.Substring(3 + 3 + 3 + 8, 8);
            Response.Write(line);
            }

            counter++;

        }

        file.Close();

所以它可能对某人有用。在这一刻,我不知道你的哪个解决方案是最好的。只是想感谢大家在这么短的时间内帮助我。

【讨论】:

    【解决方案2】:

    试试这样的

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"lista.txt";
            static void Main(string[] args)
            {
                StreamReader reader = new StreamReader(FILENAME);
                int lineCount = 0;
                string inputline = "";
                do while((inputline = reader.ReadLine()) != null)
                {
                    if(++lineCount == 13)
                    {
                        string[] inputArray = inputline.Trim().Split(new char[] {' ','\t'},StringSplitOptions.RemoveEmptyEntries);
    
                        Console.WriteLine("Exchange Rate : '{0}'", inputArray[2]);
                        break;
                    }
                }
    
            }
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      String.Split() 应该可以为所欲为。

      如果您已经拥有该列,它将是:

      var array = column.Split('\t'); //assuming the separator is a tab
      

      【讨论】:

        【解决方案4】:

        在图像中,线条似乎是制表符分隔的? 您可以读取每一行,然后使用制表符执行拆分....

        string[] arsplit = line.Split('\t'); 
        

        然后根据我想你会知道的索引从数组中获取项目

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-07-23
          • 2015-02-02
          • 2021-05-10
          • 1970-01-01
          • 2016-06-13
          相关资源
          最近更新 更多