如何高效地编辑C#字符串数组中的数据

如何高效地编辑C#字符串数组中的数据,c#,C#,我正在开发一个简单的程序来编辑字符串数组中的数据,在过去的几个晚上我一直在为这个问题挠头。我对C#比较陌生,非常感谢您的帮助 我想将字符串数组编辑成如下形式(理论上): 如果未找到该节,则应创建该节(以及另一行,其中包含传递给该方法的键和数据)。如果找到了,则应在下一节(或文件末尾)之前对其进行检查。如果在节中找不到该键,则应在节的末尾创建该键。如果找到,则应编辑键的数据 最好的方法是什么?我曾尝试过几次使用一些超级黑客代码,但最终总是得到这样的结果: [Section] Key3: Syste

我正在开发一个简单的程序来编辑字符串数组中的数据,在过去的几个晚上我一直在为这个问题挠头。我对C#比较陌生,非常感谢您的帮助

我想将字符串数组编辑成如下形式(理论上):

如果未找到该节,则应创建该节(以及另一行,其中包含传递给该方法的键和数据)。如果找到了,则应在下一节(或文件末尾)之前对其进行检查。如果在节中找不到该键,则应在节的末尾创建该键。如果找到,则应编辑键的数据

最好的方法是什么?我曾尝试过几次使用一些超级黑客代码,但最终总是得到这样的结果:

[Section]
Key3: System.String[]
抱歉,如果这不是最好的问题。正如我所说,我对C#还比较陌生,确实需要帮助。谢谢。

“编辑字符串数组中的数据”

第二个值(
myArray[1]
)已从
two
更改为
nottwo

现在更深入地描述你的问题

您已经提到了键和值,因此您很可能希望查看
字典类
。见参考资料:

例如:

Dictionary<int, string> myDictionary = new Dictionary<string, string>();
myDictionary.Add("one", "Hello");
myDictionary.Add("two", "World");
myDictionary.Add("three", "This is");
myDictionary.Add("sandwich", "a Dictionary.");

Console.Writeline(myDictionary["one"]);
Console.Writeline(myDictionary["two"]);
Console.Writeline(myDictionary["three"]);
Console.Writeline(myDictionary["sandwich"]);
Dictionary myDictionary=newdictionary();
添加(“一”,“你好”);
添加(“两个”、“世界”);
我的字典。加上(“三”,“这是”);
添加(“三明治”,“字典”);
Console.Writeline(我的字典[“一”]);
Console.Writeline(我的字典[“两个]);
Console.Writeline(我的字典[“三”]);
Console.Writeline(我的字典[“三明治]);

我发现了一些适用于我的用例的代码

public static string[] SetData(string section, string key, string value, string[] data)
        {

            var updatedData = data;
            int sectionIndex = Array.IndexOf(data, "[" + section + "]");

            if(sectionIndex > -1)
            {
                //section found
                for(int i = sectionIndex; i < data.Length; i++)
                {
                    if (data[i].StartsWith(key))
                    {
                        //key found
                        string newData = data[i];
                        string tempString = newData.Remove(newData.LastIndexOf(":"));
                        updatedData[i] = tempString + ": " + value;
                        break;     
                    }
                    else if (data[i].StartsWith("[") && !data[i].Contains(section))
                    {
                        //key not found, end of section reached.
                        List<string> temp = data.ToList();
                        temp.Insert(i, key + ": " + value);
                        updatedData = temp.ToArray();
                        break;                
                    }
                    else if (i == data.Length - 1) //-1?
                    {
                        //key not found, end of file reached.
                        List<string> temp = data.ToList();
                        temp.Insert(i, key + ": " + value);
                        updatedData = temp.ToArray();
                        break; 
                    }
                }
                return updatedData;
            }
            else
            {
                //section not found
                updatedData = new string[data.Length + 2];

                for (int i = 0; i < data.Length; i++)
                {
                    updatedData[i] = data[i];
                }

                updatedData[updatedData.Length - 2] = "[" + section + "]";
                updatedData[updatedData.Length - 1] = key + ": " + value;
                return updatedData;
            }

        }
公共静态字符串[]SetData(字符串部分、字符串键、字符串值、字符串[]数据)
{
var UpdateData=数据;
int sectionIndex=Array.IndexOf(数据,“[”+节+“]”);
如果(节索引>-1)
{
//找到的部分
for(inti=sectionIndex;i
向我们展示您的代码,以及您正在使用的数组/数据。以及您预期的输出应该是什么。阅读本文很难了解太多。“最佳方式”问题很少在堆栈溢出时表现良好,特别是当它们仅伴随伪代码时。是INI文件吗?
Dictionary<int, string> myDictionary = new Dictionary<string, string>();
myDictionary.Add("one", "Hello");
myDictionary.Add("two", "World");
myDictionary.Add("three", "This is");
myDictionary.Add("sandwich", "a Dictionary.");

Console.Writeline(myDictionary["one"]);
Console.Writeline(myDictionary["two"]);
Console.Writeline(myDictionary["three"]);
Console.Writeline(myDictionary["sandwich"]);
public static string[] SetData(string section, string key, string value, string[] data)
        {

            var updatedData = data;
            int sectionIndex = Array.IndexOf(data, "[" + section + "]");

            if(sectionIndex > -1)
            {
                //section found
                for(int i = sectionIndex; i < data.Length; i++)
                {
                    if (data[i].StartsWith(key))
                    {
                        //key found
                        string newData = data[i];
                        string tempString = newData.Remove(newData.LastIndexOf(":"));
                        updatedData[i] = tempString + ": " + value;
                        break;     
                    }
                    else if (data[i].StartsWith("[") && !data[i].Contains(section))
                    {
                        //key not found, end of section reached.
                        List<string> temp = data.ToList();
                        temp.Insert(i, key + ": " + value);
                        updatedData = temp.ToArray();
                        break;                
                    }
                    else if (i == data.Length - 1) //-1?
                    {
                        //key not found, end of file reached.
                        List<string> temp = data.ToList();
                        temp.Insert(i, key + ": " + value);
                        updatedData = temp.ToArray();
                        break; 
                    }
                }
                return updatedData;
            }
            else
            {
                //section not found
                updatedData = new string[data.Length + 2];

                for (int i = 0; i < data.Length; i++)
                {
                    updatedData[i] = data[i];
                }

                updatedData[updatedData.Length - 2] = "[" + section + "]";
                updatedData[updatedData.Length - 1] = key + ": " + value;
                return updatedData;
            }

        }