将列表转换为与字符串模式完全相同的纯字符串

时间:2018-08-23 14:05:14

标签: c#

请检查下面的代码。我正在尝试将项目列表转换为具有特定字符串模式的纯字符串,您将在代码注释中找到该模式。一件事是在转换过程中,我还需要删除ASIN属性:之前的所有空格和任何字符。如您所见,我已经尝试过.ToString(),但这无法转换列表值。知道我该怎么做吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            //asign some demo data
            var tpd = new List<ThirdPartyData>();

            tpd.Add(new ThirdPartyData { ASIN = "mark: NsQf8", type = 0, Price = 7, IsPrime = 1, OutOfStock = 1 });
            tpd.Add(new ThirdPartyData { ASIN = "fD5tsQ ", type = 1, Price = 13, IsPrime = 0, OutOfStock = 0 });
            tpd.Add(new ThirdPartyData { ASIN = "notmark: tvQtsu", type = 1, Price = 14, IsPrime = 1, OutOfStock = 1 });

            //this tpd list should be converted string like bellow-

            //Converted String output should be like this- 'ASIN-NsQf8,type-0,Price-7,IsPrime-1,OutOfStock-1:ASIN-fD5tsQ,type-1,Price-13,IsPrime-0,OutOfStock-0:ASIN-tvQtsu,type-1,Price-14,IsPrime-1,OutOfStock-1'

            string output = tpd.ToString();//i tried it not works

        }

    }

    class ThirdPartyData
    {
        public string ASIN { get; set; }
        public int type { get; set; }
        public int Price { get; set; }
        public int IsPrime { get; set; }
        public int OutOfStock { get; set; }

    }
}

5 个答案:

答案 0 :(得分:3)

首先,您定义一种将自定义类转换为所需的字符串表示形式的方法:

class ThirdPartyData
{
    public string ASIN { get; set; }
    public int type { get; set; }
    public int Price { get; set; }
    public int IsPrime { get; set; }
    public int OutOfStock { get; set; }

    public string ToExport()
    {
        return $"ASIN-{ASIN},type-{type},...";
    }
}

然后将您的类实例映射到它们的字符串表示形式:

string[] tpdStrings = tpd.Select(t => t.ToExport()).ToArray();

然后使用所需的定界符将这些字符串连接起来:

string output = String.Join(":", tpdStrings);

答案 1 :(得分:1)

一个想法是编写一个方法,该方法接受一个$对象,并返回按您指定的格式设置的属性名称和值的字符串(此代码也可以放在{{1 ThirdPartyData类中的}}方法(如果您可以控制该类,并认为这是该类的最佳字符串表示形式):

ToString()

然后,您可以简单地将输出设置为使用ThirdPartyData字符连接每个项目的字符串的结果:

private static string TpdToString(ThirdPartyData input)
{
    if (input == null) return null;
    var asinParts = input.ASIN?.Split(':') ?? new[] {"[NULL]"};
    var asin = asinParts.Length > 1 ? asinParts[1].Trim() : asinParts[0].Trim();

    return $"ASIN-{asin},type-{input.type},Price-{input.Price}," + 
        $"IsPrime-{input.IsPrime},OutOfStock-{input.OutOfStock}";
}

样品

':'

输出

enter image description here

答案 2 :(得分:0)

您可以按照下面的代码所示进行操作:覆盖Tostring,并且还可以使用string.join在main方法中创建您的字符串。我还建议您使用正则表达式清除所有空白,如下所示:

class Program
    {
        static void Main(string[] args)
        {

            //asign some demo data
            var tpd = new List<ThirdPartyData>();

            tpd.Add(new ThirdPartyData { ASIN = "mark: NsQf8", type = 0, Price = 7, IsPrime = 1, OutOfStock = 1 });
            tpd.Add(new ThirdPartyData { ASIN = "fD5tsQ ", type = 1, Price = 13, IsPrime = 0, OutOfStock = 0 });
            tpd.Add(new ThirdPartyData { ASIN = "notmark: tvQtsu", type = 1, Price = 14, IsPrime = 1, OutOfStock = 1 });

            //this tpd list should be converted string like bellow-

            //Converted String output should be like this- 'ASIN-NsQf8,type-0,Price-7,IsPrime-1,OutOfStock-1:ASIN-fD5tsQ,type-1,Price-13,IsPrime-0,OutOfStock-0:ASIN-tvQtsu,type-1,Price-14,IsPrime-1,OutOfStock-1'


            var strings = new List<string>();
            tpd.ForEach(item => strings.Add(item.ToString()));
            string output =string.Join(",", strings);
        }

    }

    class ThirdPartyData
    {
        readonly Regex WhiteSpaceRegex = new Regex("[ ]", RegexOptions.Compiled);
        public string ASIN { get; set; }
        public int type { get; set; }
        public int Price { get; set; }
        public int IsPrime { get; set; }
        public int OutOfStock { get; set; }

        public override string ToString()
        {
            string[] asin = ASIN.Split(':');
            string asinString = string.Empty;
            if (asin.Length >= 1)
            {
                asinString = WhiteSpaceRegex.IsMatch(asin[1]) ? WhiteSpaceRegex.Replace(asin[1], string.Empty).Trim() : asin[1]; ;
            }
            return $"ASIN-{asinString},type-{type},Price-{Price},IsPrime-{IsPrime},OutOfStock-{OutOfStock}";
        }

答案 3 :(得分:0)

您可以使用此代码,但我认为您必须在类中实现ToString()。

string output = string.Join(
      ",",
      Array.ConvertAll<ThirdPartyData, string>(tpd, Convert.ToString)
    );

答案 4 :(得分:0)

希望这会有所帮助。缺少的部分是ThirdPartyData对象到String的投影。之后,您只需要连接这些字符串即可。就是这样。

static void Main(string[] args)
{

        //asign some demo data
        var tpd = new List<ThirdPartyData>();

        tpd.Add(new ThirdPartyData { ASIN = "mark: NsQf8", type = 0, Price = 7, IsPrime = 1, OutOfStock = 1 });
        tpd.Add(new ThirdPartyData { ASIN = "fD5tsQ ", type = 1, Price = 13, IsPrime = 0, OutOfStock = 0 });
        tpd.Add(new ThirdPartyData { ASIN = "notmark: tvQtsu", type = 1, Price = 14, IsPrime = 1, OutOfStock = 1 });

        //this tpd list should be converted string like bellow-

        //Converted String output should be like this- 'ASIN-NsQf8,type-0,Price-7,IsPrime-1,OutOfStock-1:ASIN-fD5tsQ,type-1,Price-13,IsPrime-0,OutOfStock-0:ASIN-tvQtsu,type-1,Price-14,IsPrime-1,OutOfStock-1'

        var individual = tpd.Select(t => $"ASIN-{ (t.ASIN.Split(new char[]{ ':' }).Length == 1 ? t.ASIN.Split(new char[] { ':' })[0].Trim() : t.ASIN.Split(new char[] { ':' })[1].Trim()) },type-{t.type},Price-{t.Price},IsPrime-{t.IsPrime},OutOfStock-{t.OutOfStock}").ToArray();
        var output = string.Join(",", individual);

        Console.WriteLine(output);
        Console.ReadLine();

}