从C#Visual Studio中的字符串中提取值

发布于 2025-01-17 18:11:07 字数 502 浏览 0 评论 0原文

public static string GetMacQ(string rawValues)
    {
        return rawValues.Split('#')
        .Select(element =>
        {
            var value = element.Split('='); 
            return new 
            {
                Key = value[0],
                Value = value[1],
            };
        })
        .Where(element => element.Key.Equals("Mac Q")) 
        .Select(element => element.Value) 
        .FirstOrDefault();  
    }

这段代码用于分割字符串,但这里的问题是它不返回键的值

public static string GetMacQ(string rawValues)
    {
        return rawValues.Split('#')
        .Select(element =>
        {
            var value = element.Split('='); 
            return new 
            {
                Key = value[0],
                Value = value[1],
            };
        })
        .Where(element => element.Key.Equals("Mac Q")) 
        .Select(element => element.Value) 
        .FirstOrDefault();  
    }

This code is for splitting the strings, but the problem here is it does not return the value of the key

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2025-01-24 18:11:07

使用linq

您可以检查/运行 https://dotnetfiddle.net/pytuul

完整示例代码

using System;
using System.Linq;
using System.Collections.Generic;

namespace License
{
    public static class Utils
    {
        public static string GetValue(string key, string rawValues)
        {

            return rawValues.Split("[##]") // Split the string with [##] separator. e.g. ["C=True", "V=True", "PAN=True", "Mac Q=3", "A=True"];
            .Select(element => //Using the extension function SelectWhere declared above.
            {
                var value = element.Split("="); //Split the string with = separator. e.g. / ["C", "True"] / ["PAN", "True"] / ["Mac Q", "3"]
                return new //Creating new anonymous object with Key and Value properties. e.g { Key : "Mac Q", Value: "3"}
                {
                    Key = value[0].Trim(),
                    Value = value.Length < 2 ? string.Empty : value[1] //Check if element has 2 elements after split. if length < 2 value is empty string.
                };
            })
            .Where(element => element.Key.Equals(key)) //Filter elements with key value
            .Select(element => element.Value) //Select the value
            .FirstOrDefault();  //Get the first ocurrence or null.
        }

    }

    public class Program
    {
        public static void Main(string[] args)
        {
            string key = "Mac Q";

            string rawValues = "C=True[##]V=True[##]PAN=True[##]Mac Q[##]A=True"; //Mac Q is empty
            string value = Utils.GetValue(key, rawValues); //Calling to the function
            Console.WriteLine(value == null ? $@"""{key}"" value not found." : $@"Mac Q value is ""{value}""");

            rawValues = "C=True[##]V=True[##]PAN=True[##]A=True"; //Mac Q is not present.
            value = Utils.GetValue(key, rawValues); 
            Console.WriteLine(value == null ? $@"""{key}"" value not found." : $@"Mac Q value is ""{value}""");

            rawValues = "C=True[##]V=True[##]PAN=True[##]Mac Q=3[##]A=True"; //Mac Q is present with value
            value = Utils.GetValue(key, rawValues); 
            Console.WriteLine(value == null ? $@"""{key}"" value not found." : $@"Mac Q value is ""{value}""");

            Console.ReadLine();
        }
    }
}

输出

Mac Q value is ""
"Mac Q" value not found.
Mac Q value is "3"

Using Linq

You can check/run this code on https://dotnetfiddle.net/pYtUUl

Full example code

using System;
using System.Linq;
using System.Collections.Generic;

namespace License
{
    public static class Utils
    {
        public static string GetValue(string key, string rawValues)
        {

            return rawValues.Split("[##]") // Split the string with [##] separator. e.g. ["C=True", "V=True", "PAN=True", "Mac Q=3", "A=True"];
            .Select(element => //Using the extension function SelectWhere declared above.
            {
                var value = element.Split("="); //Split the string with = separator. e.g. / ["C", "True"] / ["PAN", "True"] / ["Mac Q", "3"]
                return new //Creating new anonymous object with Key and Value properties. e.g { Key : "Mac Q", Value: "3"}
                {
                    Key = value[0].Trim(),
                    Value = value.Length < 2 ? string.Empty : value[1] //Check if element has 2 elements after split. if length < 2 value is empty string.
                };
            })
            .Where(element => element.Key.Equals(key)) //Filter elements with key value
            .Select(element => element.Value) //Select the value
            .FirstOrDefault();  //Get the first ocurrence or null.
        }

    }

    public class Program
    {
        public static void Main(string[] args)
        {
            string key = "Mac Q";

            string rawValues = "C=True[##]V=True[##]PAN=True[##]Mac Q[##]A=True"; //Mac Q is empty
            string value = Utils.GetValue(key, rawValues); //Calling to the function
            Console.WriteLine(value == null ? $@"""{key}"" value not found." : $@"Mac Q value is ""{value}""");

            rawValues = "C=True[##]V=True[##]PAN=True[##]A=True"; //Mac Q is not present.
            value = Utils.GetValue(key, rawValues); 
            Console.WriteLine(value == null ? $@"""{key}"" value not found." : $@"Mac Q value is ""{value}""");

            rawValues = "C=True[##]V=True[##]PAN=True[##]Mac Q=3[##]A=True"; //Mac Q is present with value
            value = Utils.GetValue(key, rawValues); 
            Console.WriteLine(value == null ? $@"""{key}"" value not found." : $@"Mac Q value is ""{value}""");

            Console.ReadLine();
        }
    }
}

Output

Mac Q value is ""
"Mac Q" value not found.
Mac Q value is "3"
无妨# 2025-01-24 18:11:07

这是代码:

// The string to be parsed.As can be seen in the string, this is some key/value pairs, which are separated by [##]
var subkey = "C=True[##]V=True[##]PAN=True[##]Mac Q=3[##]A=True";

// Here we split the string by the separator [##]
var pairs = subkey.Split("[##]");

// Now let's create a storage to store the key/values
var dic = new Dictionary<string, string>();

foreach (var pair in pairs)
{
    // The keys and values are separated by "=". So we split them by the separator.
    var split = pair.Split("=");
    // Now let's add the to the storage
    dic.Add(split[0], split[1]);
}

// OK. done. Let's verify the result.
foreach(var item in dic)
{
    Console.WriteLine($"{item.Key}={item.Value}");
}

Here is the code:

// The string to be parsed.As can be seen in the string, this is some key/value pairs, which are separated by [##]
var subkey = "C=True[##]V=True[##]PAN=True[##]Mac Q=3[##]A=True";

// Here we split the string by the separator [##]
var pairs = subkey.Split("[##]");

// Now let's create a storage to store the key/values
var dic = new Dictionary<string, string>();

foreach (var pair in pairs)
{
    // The keys and values are separated by "=". So we split them by the separator.
    var split = pair.Split("=");
    // Now let's add the to the storage
    dic.Add(split[0], split[1]);
}

// OK. done. Let's verify the result.
foreach(var item in dic)
{
    Console.WriteLine(
quot;{item.Key}={item.Value}");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文