根据值将单列列表(字符串)()解析为两列列表

本文关键字:列表 两列 字符串 单列 | 更新日期: 2023-09-27 18:36:51

我有以下列表(字符串)()

buttons=10
button_exit=10
button_x=600
button_y=400
;
button_text_01=Start
button_program_01=Start.exe
button_bckcolor_01=&H0080FFFF
;
button_text_02=Second
button_program_02=Second.exe
button_bckcolor_02=&H00FFFF80
;
button_text_03=Load
button_program_03=Load.exe
button_bckcolor_03=&H0080C0FF

我想在新列表中添加一些值,例如:

Column1  Column2
------   -----
Start    Start.exe
Second   Second.exe
Load     Load.exe

所以,我唯一需要的值是 button_text_xxbutton_program_xx .有什么想法吗?

下面是我的错误代码尝试。

If listSCMenuSection.Count > 0 Then
    For Each item In listSCMenuSection
        ' MsgBox(listSCMenuSection.IndexOf(item).ToString)
        If item.Contains("button_caption_") Then
            dtButtons.Rows.Add(item.ToString, 0)
        End If
    Next
'Loop through
For Each item In listSCMenuSection
    If item.Contains("button_program_") Then
        Dim index As Integer = -1
        Dim sIndex As String
        For i As Integer = 0 To dtButtons.Rows.Count
            If i.ToString.Count < 10 Then
                sIndex = "0" & i.ToString
            Else
                sIndex = i.ToString
            End If
            Dim rows As DataRow() = dtButtons.[Select]("Name Like '%" & sIndex & "%'")
            If rows.Count = 1 Then
                index = dtButtons.Rows.IndexOf(rows(0))
                'Add the button_program to the corresponding index of button_caption
                dtButtons.Rows(index)(1) = item.ToString
            ElseIf rows.Count > 1 Then
                MessageBox.Show("You have more than one buttons declared in the ini file for: " & item.ToString)
                Exit Sub
            End If
        Next
        'If listSCMenuSection.IndexOf(item).ToString.Count < 10 Then
        '    sIndex = "0" & listSCMenuSection.IndexOf(item).ToString
        'Else
        '    sIndex = listSCMenuSection.IndexOf(item).ToString
        'End If
    End If
    Next
    Dim x As Integer = 0
Else
    MessageBox.Show("Please configure the 'MENU' section in the Ini file", "Section keys missing", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If

根据值将单列列表(字符串)()解析为两列列表

你想要解析字符串列表中的数据。它似乎是从文本文件加载的,格式类似于 ini 文件。"buttons=X"定义的行有 X 按钮。

一种方法如下:

  • 读取按钮数量
  • 从 1 到按钮数循环
  • 对于每个button_n
  • 尝试查找三个值(文本、程序、背景)中的每一个,并将按钮添加到列表中

这可以这样实现:

using System;
using System.Linq;
using System.Collections.Generic;
public class ButtonEntry
{
    public int Number { get; set; } 
    public string Text { get; set; }
    public string Program { get; set; }
    public string BackgroundColor { get; set; }
}
public class Program
{
    public static void Main()
    {
        // Load data...
        var data = new List<string>
        {
            "buttons=2",
            ";",
            "button_text_01=a",
            "button_text_02=b",
            "button_program_02=p",
            "button_bckcolor_02=c",
        };
        // Find number of buttons.
        string buttonString = ReadData(data, "buttons");
        int buttonCount = int.Parse(buttonString);
        var buttons = new List<ButtonEntry>();
        // Fill each button.
        for (int b = 1; b <= buttonCount; b++)
        {
            var button = new ButtonEntry
            {
                Number = b
            };
            string needle = "button_text_" + b.ToString("D2");  
            button.Text = ReadData(data, needle);
            needle = "button_program_" + b.ToString("D2");  
            button.Program = ReadData(data, needle);
            needle = "button_bckcolor_" + b.ToString("D2"); 
            button.BackgroundColor = ReadData(data, needle);
            buttons.Add(button);
        }
        foreach (var button in buttons)
        {
            Console.WriteLine("Button {0}: {1}, {2}, {3}", button.Number, button.Text, button.Program, button.BackgroundColor);
        }
    }
    /// <summary>
    /// Finds the line in <paramref name="data" /> that starts with
    /// <paramref name="needle" /> followed by "=", returning the 
    /// value after "=" or null when not found.
    /// </summary>
    public static string ReadData(List<string> data, string needle)
    {
        var line = data.FirstOrDefault(s => s.StartsWith(needle));
        if (line == null)
        {
            return null;
        }
        return line.Substring(line.IndexOf("=") + 1);
    }
}

指纹:

Button 1: a, , 
Button 2: b, p, c

这不会进行任何错误处理,并且性能不佳,因为它每次都会循环整个列表以查找给定的条目,但鉴于列表保持较小,它可以被认为是"足够好"。如果你不想要 C#,你不应该这样标记问题。