c#两个字符串数组将第一个数组值打印到第二个数组值expect 0

时间:2015-12-02 05:11:57

标签: c# arrays string

我希望输出如下:

A=1
C=3
D=9
e=5

如果第二个数组值为0,我不希望该值如何在c#中实现此输出?

private static void Main(string[] args)
{
    string[] a = new string[] { "a", "b", "c", "d" ,"e"};
    string[] b = new string[] {1,0,3,0,5 };

    foreach (string tmp in a)
    {
        bool existsInB = false;
        foreach (string tmp2 in b)
        {
             if (tmp == tmp2)
             {
                 existsInB = true;
                 break;
             }
         }

         if (!existsInB)
         {
             Console.WriteLine(string.Format("{0} is not in b", tmp));
         }
     }

     Console.ReadLine();
}

在那两个数组中,我想打印像a=1,c=3,e=5这样的值,我不想打印第二个数组零值。我如何实现这一目标?

我需要c#中的输出:a=1 c=3 e=5

2 个答案:

答案 0 :(得分:3)

我相信这对你有用

$ cat Dockerfile

# get Ubuntu 14.04 images
FROM ubuntu:14.04

# copy file on local to images
#COPY erp-enterprise_revisi.tar.gz /home/

# Update repository
RUN apt-get update

# Install mc on Ubuntu 14.04
RUN apt-get install -y mc         <== Here you need give -y to force the install

$ docker build -no-cache -t test .

字典版(根据马特·默多克的建议):

class Program
{
    static void Main(string[] args)
    {
        string[] a = new string[] { "a", "b", "c", "d", "e" };
        int[] b = new int[] { 1, 0, 3, 0, 5 };

        for (int i = 0; i < a.Length; i++)
        {
            if (b[i] != 0)
                Console.WriteLine(a[i] + "=" + b[i]);
        }

        Console.ReadLine();
    }
}

答案 1 :(得分:0)

@interceptwind的另一个答案已经给出了解决方案,这只是Linq的替代解决方案

string[] a = new string[] { "a", "b", "c", "d", "e" };
string[] b = new string[] { "1", "0", "3", "9", "5" };

var result = a.Zip(b, (strA, strB) => string.Format("{0}={1}", strA.ToUpper(), strB))
                .Where(s => !s.Contains("=0"))
                .ToArray();