简体   繁体   English

如何在 C# 中拆分字符串?

[英]How to split a string in C#?

I am trying to parse the following string and get the result.我正在尝试解析以下字符串并获得结果。

string test = "SiteA:Pages:1,SiteB:Pages:4,SiteA:Documents:6"

I am trying to get the following result after the split.我试图在拆分后得到以下结果。

string SiteA = "Pages:1,Documents:6"
string SiteB = "Pages:4"

Here is my code but it doesn't seem to be working.这是我的代码,但它似乎不起作用。 How can I get all related "SiteA" and "SiteB"?如何获得所有相关的“SiteA”和“SiteB”?

List<string> listItem = new List<string>();
string[] keyPairs = test.Split(',');
string[] item;
foreach (string keyPair in keyPairs)
{
    item = keyPair.Split(':');
    listItem.Add(string.Format("{0}:{1}", item[0].Trim(), item[1].Trim()));
}

I would use a Lookup for this:我会为此使用Lookup

string test = "SiteA:Pages:1,SiteB:Pages:4,SiteA:Documents:6";
var listItemsBySite = test.Split(',')
                          .Select(x => x.Split(':'))
                          .ToLookup(x => x[0], 
                                    x => string.Format("{0}:{1}", 
                                                       x[1].Trim(), 
                                                       x[2].Trim()));

You can then use it like this:然后你可以像这样使用它:

foreach (string item in listItemsBySite["SiteA"])
{
    Console.WriteLine(item);
}

Here's my solution... pretty elegant in LINQ, you can use anonymous objects, Tuples, KeyValuePair, or your own custom class.这是我的解决方案...在 LINQ 中非常优雅,您可以使用匿名对象、元组、KeyValuePair 或您自己的自定义 class。 I'm just using an anonymous type.我只是使用匿名类型。

string test = "SiteA:Pages:1,SiteB:Pages:4,SiteA:Documents:6";

            var results = test
                .Split(',')
                .Select(item => item.Split(':'))
                .ToLookup(s => s[0], s => new { Key = s[1], Value = s[2] });

            // This code just for display purposes
            foreach (var site in results)
            {
                Console.WriteLine("Site: " + site.Key);

                foreach (var value in site)
                {
                    Console.WriteLine("\tKey: " + value.Key + " Value: " + value.Value);
                }
            }

Here is my code:这是我的代码:

string test = "SiteA:Pages:1,SiteB:Pages:4,SiteA:Documents:6";
string[] data = test.Split(',');
Dictionary<string, string> dic = new Dictionary<string, string>();
for(int i = 0; i < data.Length; i++) {
    int index = data[i].IndexOf(':');
    string key = data[i].Substring(0, index);
    string value = data[i].Substring(index + 1);
    if(!dic.ContainsKey(key))
        dic.Add(key, value);
    else
        dic[key] = string.Format("{0}, {1}", new object[] { dic[key], value }); 
}

Here is how I would do it:这是我的做法:

SortedList<string, StringBuilder> listOfLists = new SortedList<string, StringBuilder>();
string[] keyPairs = test.Split(',');
foreach (string keyPair in keyPairs)
{
    string[] item = keyPair.Split(':');
    if (item.Length >= 3)
    {
        string nextValue = string.Format("{0}:{1}", item[1].Trim(), item[2].Trim());
        if (listOfLists.ContainsKey(item[0]))
            listOfLists[item[0]].Append(nextValue);
        else
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(nextValue);
            listOfLists.Add(item[0], sb);
        }
    }
}

foreach (KeyValuePair<string, StringBuilder> nextCollated in listOfLists)
    System.Console.WriteLine(nextCollated.Key + ":" + nextCollated.Value.ToString());

This is what I would do (tested).这就是我会做的(测试)。

(However, does assume that all items will be correctly formatted). (但是,假设所有项目都将正确格式化)。 And of course, it's not really optimized.当然,它并没有真正优化。

static void Main(string[] args)
{

    string test = "SiteA:Pages:1,SiteB:Pages:4,SiteA:Documents:6";

    Dictionary<String, List<String>> strings = new Dictionary<string, List<string>>();

    String[] items = test.Split(',');

    foreach (String item in items)
    {
        List<String> itemParts = item.Split(':').ToList();
        String firstPart = itemParts[0];

        itemParts.RemoveAt(0);
        String secondPart = String.Join(":", itemParts);

        if (!strings.ContainsKey(firstPart))
            strings[firstPart] = new List<string>();

        strings[firstPart].Add(secondPart);
    }


    // This is how you would consume it
    foreach (String key in strings.Keys)
    {
        List<String> keyItems = strings[key];
        Console.Write(key + ": ");

        foreach (String item in keyItems)
            Console.Write(item + " ");
        Console.WriteLine();
    }

}

Here's a solution using LINQ:这是使用 LINQ 的解决方案:

string test = "SiteA:Pages:1,SiteB:Pages:4,SiteA:Documents:6";

var dict = test
           .Split(',')
           .GroupBy(s => s.Split(':')[0])
           .ToDictionary(g => g.Key,
                         g => string.Join(",", 
                                  g.Select(i => string.Join(":", i.Split(':').Skip(1)))
                                   .ToArray()));
using System;
using System.Linq;
using System.Text.RegularExpressions;

class MyClass
{
   static void Main(string[] args)
   {
       string test = "SiteA:Pages:1,SiteB:Pages:4,SiteA:Documents:6";
       var sites = test.Split(',')
           .Select(p => p.Split(':'))
           .Select(s => new { Site = s[0], Key = s[1], Value = s[2] })
           .GroupBy(s => s.Site)
           .ToDictionary(g => g.Key, g => g.ToDictionary(e => e.Key, e => e.Value));

       foreach (var site in sites)
           foreach (var key in site.Value.Keys)
               Console.WriteLine("Site {0}, Key {1}, Value {2}", site.Key, key, site.Value[key]);

       // in your preferred format:
       var SiteA = string.Join(",", sites["SiteA"].Select(p => string.Format("{0}:{1}", p.Key, p.Value)));
       var SiteB = string.Join(",", sites["SiteB"].Select(p => string.Format("{0}:{1}", p.Key, p.Value)));

       Console.WriteLine(SiteA);
       Console.WriteLine(SiteB);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM