从文本文件中获取字符串中的特定值

时间:2015-11-09 14:15:28

标签: c# winforms

我在我的文本文件中有这个:

[Log] <?php  (localhost, line 23)
  $output = "LALALALALALALALALALALALALA"
  echo $output;
?> 

000000010:鸡:$ 8.99:的 345 :2015年3月11日:2016年3月11日:$ 7.99

我需要获得每个&#34;数量&#34;的价值。来自粗体位置的值。

编辑: 我还要比较我得到的值,如果数量很少,我会给出错误。

7 个答案:

答案 0 :(得分:1)

在输入数据较大的情况下,内存消耗最小的解决方案。 另外:数量列中没有处理不正确的数据。要做到这一点,只需替换 int.Parse 块;

这是使用LINQ表达式处理文件数据的几种方法

    internal static class MyExtensions
{
    /// <exception cref="OutOfMemoryException">There is insufficient memory to allocate a buffer for the returned string. </exception>
    /// <exception cref="IOException">An I/O error occurs. </exception>
    /// <exception cref="ArgumentException"><paramref name="stream" /> does not support reading. </exception>
    /// <exception cref="ArgumentNullException"><paramref name="stream" /> is null. </exception>
    public static IEnumerable<string> EnumerateLines(this Stream stream)
    {
        using (var reader = new StreamReader(stream))
        {
            do
            {
                var line = reader.ReadLine();
                if (line == null) break;
                yield return line;
            } while (true);
        }
    }

    /// <exception cref="ArgumentNullException"><paramref name="line"/> is <see langword="null" />.</exception>
    public static IEnumerable<string> ChunkLine(this string line)
    {
        if (line == null) throw new ArgumentNullException("line");
        return line.Split(':');
    }

    /// <exception cref="ArgumentNullException"><paramref name="chuckedData"/> is <see langword="null" />.</exception>
    /// <exception cref="ArgumentException">Index should be not negative value</exception>
    public static string GetColumnData(this IEnumerable<string> chuckedData, int columnIndex)
    {
        if (chuckedData == null) throw new ArgumentNullException("chuckedData");
        if (columnIndex < 0) throw new ArgumentException("Column index should be >= 0", "columnIndex");
        return chuckedData.Skip(columnIndex).FirstOrDefault();
    }
}

这是用法示例:

    private void button1_Click(object sender, EventArgs e)
    {
        var values = EnumerateQuantityValues("largefile.txt");
        // do whatever you need
    }

    private IEnumerable<int> EnumerateQuantityValues(string fileName)
    {
        const int columnIndex = 3;
        using (var stream = File.OpenRead(fileName))
        {
            IEnumerable<int> enumerable = stream
                .EnumerateLines()
                .Select(x => x.ChunkLine().GetColumnData(columnIndex))
                .Select(int.Parse);

            foreach (var value in enumerable)
            {
                yield return value;
            }                
        }
    }

答案 1 :(得分:0)

考虑一下你是否设法在字符串数组或列表中获取所有这些行。

您可以应用以下代码将数量集合作为IEnumerable<string>

var quantity = arr.Select(c =>
{
    var temp = c.Split('$');
    if (temp.Length > 1)
    {
        temp = temp[1].Split(':');
        if (temp.Length > 1)
        {
            return temp[1];
        }
    }
    return null;
}).Where(c => c != null);

<强>更新

检查小提琴。 https://dotnetfiddle.net/HqKdeI

答案 2 :(得分:0)

你只需要拆分字符串

string data = @"000000000:Carrots:$1.99:214:03/11/2015:03/11/2016:$0.99
000000001:Bananas:$1.99:872:03/11/2015:03/11/2016:$0.99
000000002:Chocolate:$2.99:083:03/11/2015:03/11/2016:$1.99
000000003:Spaghetti:$3.99:376:03/11/2015:03/11/2016:$2.99
000000004:Tomato Sauce:$1.99:437:03/11/2015:03/11/2016:$0.99
000000005:Lettuce:$0.99:279:03/11/2015:03/11/2016:$0.99
000000006:Orange Juice:$2.99:398:03/11/2015:03/11/2016:$1.99
000000007:Potatoes:$2.99:792:03/11/2015:03/11/2016:$1.99
000000008:Celery:$0.99:973:03/11/2015:03/11/2016:$0.99
000000009:Onions:$1.99:763:03/11/2015:03/11/2016:$0.99
000000010:Chicken:$8.99:345:03/11/2015:03/11/2016:$7.99";

string[] rows = data.split(Environment.Newline.ToCharArray());

foreach(var row in rows)
{
    string[] cols = row.Split(':');
    var quantity = cols[3];
}

答案 3 :(得分:0)

您可以使用String.Split执行此操作。

// Read all lines into an array
string[] lines = File.ReadAllLines(@"C:\path\to\your\file.txt");

// Loop through each one
foreach (string line in lines)
{
    // Split into an array based on the : symbol
    string[] split = line.Split(':');

    // Get the column based on index
    Console.WriteLine(split[3]);
}

答案 4 :(得分:0)

查看下面的示例代码。您关心的字符串名为theValueYouWantInTheString。

char[] delimiterChar = { ':' };
string input = @"000000010:Chicken:$8.99:345:03/11/2015:03/11/2016:$7.99";
string[] values = input.Split(delimiterChar);
string theValueYouWantInTheString = values[3];

答案 5 :(得分:0)

如果您遇到问题,请使用正则表达式。现在你有两个问题。

这是一个将您的输入用作txt文件的程序。函数GetQuantity返回一个包含数量的int的列表。使用此方法,您可以定义更多组以从每行提取信息。

namespace RegExptester
{
    class Program
    {

        private static List<int> GetQuantity(string txtFile)
        {
            string tempLineValue;
            Regex regex = new Regex(@"[0-9]*:[a-zA-Z]*:\$[0-9]*\.[0-9]*:([0-9]*).*", RegexOptions.Compiled);
            List<int> retValue = new List<int>();
            using (StreamReader inputReader = new StreamReader(txtFile))
            {
                while (null != (tempLineValue = inputReader.ReadLine()))
                {
                    Match match = regex.Match(tempLineValue);
                    if (match.Success)
                    {
                       if(match.Groups.Count == 2)
                       {
                           int numberValue;
                           if (int.TryParse(match.Groups[1].Value, out numberValue))
                               retValue.Add(numberValue);
                       }
                    }
                }
            }
            return retValue;
        } 

        static void Main(string[] args)
        {
            var tmp = GetQuantity("c:\\tmp\\junk.txt");
        }
    }
}

答案 6 :(得分:0)

显然,从每一行开始,你想要第3和第4个冒号之间的部分。 Linq可以为你做到这一点:

using (var textReader = new StreamReader(fileName))
{
    // read all text and divide into lines:
    var allText = textReader.ReadToEnd();
    var allLines = textReader.Split(new char[] {'\r','\n'}, StringSplitIoptions.RemoveEmptyEntries);

     // split each line based on ':', and take the fourth element
     var myValues = allLines.Select(line => line.Split(new char[] {':'})
         .Skip(3)
         .FirstOrDefault();
}

如果您想要较低的可读性,当然可以将这些语句连接成一行。