【问题标题】:how to separately store constituents of a string array in c#如何在c#中分别存储字符串数组的组成部分
【发布时间】:2020-01-09 21:39:00
【问题描述】:

所以,我用随机单词制作了一个字符串数组。 我想知道如何将构成这个数组的所有字符串存储到单独的字符串变量中。

public static void Main(string[] args)
    {
        string[] names1 = new string[] { "Name", "Dog", "Cat" };
        string[] names2 = new string[] { "Ocelot", "Picture", "Stark" };    
    }

正如我在标题中解释的那样,如果我可以将它们存储为单独的字符串变量,我希望它。

【问题讨论】:

  • 你的意思是string ocelot = "Ocelot";之类的?
  • 为什么要制作这些字符串数组?您能解释一下为什么要将这些字符串分配给变量,因为您已经将它们存储?你想做什么?你不能只按索引(names1[1] -> "Dog")访问字符串吗?
  • 使用字典作为字符串名称怎么样?
  • 你想把这两个数组合并成一个对象还是有别的想法?

标签: c# arrays string methods


【解决方案1】:

如果要将两个数组中的所有字符串存储到一个对象中,可以使用Concat()ToList() 方法将两个数组合并为一个List<string>

static void Main(string[] args)
{
    string[] names1 = new string[] { "Name", "Dog", "Cat" };
    string[] names2 = new string[] { "Ocelot", "Picture", "Stark" };

    List<string> stringList = names1.Concat(names2).ToList();

    Console.WriteLine(stringList[3]);// writes Ocelot to the console
}

或者如果您想通过字典键(例如stringDict["Ocelot"])按字符串值(例如“Ocelot”)访问字符串,您可以使用Concat()ToDictionary() 将所有字符串添加到Dictionary&lt;string,string&gt;

static void Main(string[] args)
{
    string[] names1 = new string[] { "Name", "Dog", "Cat" };
    string[] names2 = new string[] { "Ocelot", "Picture", "Stark" };

    Dictionary<string, string> stringDict = names1.Concat(names2).ToDictionary(x=>x);

    Console.WriteLine(stringDict["Ocelot"]);// writes Ocelot to the console
}

【讨论】:

  • 感谢您的回答。虽然我不了解字典,但我用 concat 理解了您回答的第一部分
  • 字典表示键和值的集合。基本上,您将数据存储为键值对,例如 var dictionary = new Dictionary();是一个字典,其键为字符串变量,值存储为 int。例如,您可以存储一个键值对,如 dictionary.Add("cat", 2);然后通过使用 key as dictionary["cat"] 获取值 2.Here 有几个链接可以了解更多信息:docs.microsoft.com/en-us/dotnet/api/… ; dotnetperls.com/dictionary
  • 感谢您的回复。我想确认一下,我们可以制作两个具有相同键名的字典吗?
  • 一个字典不能有重复的键,但是你可以创建两个具有相同键的独立字典。
【解决方案2】:

不可以在c#中创建动态变量,虽然数组是专门用于存储多个字符串变量的(我们使用索引来获取特定值)。我们可以实现它的另一种方法是使用键值对,即字典对象,如下所示。

static void Main(string[] args)
{
    string[] names1 = new string[] { "Name", "Dog", "Cat" };
    string[] names2 = new string[] { "Ocelot", "Picture", "Stark" };

    Dictionary<string, string> stringValues = new Dictionary<string, string>();


    for (int i = 0; i < names1.Length; i++)
    {
        stringValues.Add(String.Format(names1[i] + "{0}", i.ToString()), names1[i]);
    }

    for (int i = 0; i < names2.Length; i++)
    {
        stringValues.Add(String.Format(names2[i] + "{0}", i.ToString()), names2[i]);
    }

    foreach (KeyValuePair<string, string> val in stringValues)
    {
        Console.WriteLine(string.Format("Key = {0}, Value = {1}", val.Key, val.Value));
    }

    Console.ReadLine();

}

你会得到输出

 Key = Name0, Value = Name
    Key = Dog1, Value = Dog
    Key = Cat2, Value = Cat
    Key = Ocelot0, Value = Ocelot
    Key = Picture1, Value = Picture
    Key = Stark2, Value = Stark

这里可以根据key访问变量

【讨论】:

  • 感谢您澄清我无法创建变量来存储数组的各个部分。我只是对是否可以或不能将它们存储在单独的变量中感兴趣。另外,能否请您告诉我您使用的这些词典。我是编程和 c# 的新手,所以真的很有帮助
  • 您好,您可以点击给定的链接。 docs.microsoft.com/en-us/dotnet/api/…。这将有助于理解 C# 中的字典
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
  • 2017-03-07
相关资源
最近更新 更多