用=符号

时间:2019-03-09 06:37:46

标签: c# parsing connection-string

我正在尝试解析连接字符串并提取要在代码中使用的值。问题是我的连接字符串中的值之一带有=符号。

这是我的代码

private void Parse(string connectionString)
{
    var components = connectionString.Split(';');
    foreach (var component in components)
    {
        if (string.IsNullOrEmpty(component)) continue;

        var fieldValuePair = component.Trim().Split('=');//Problematic? Don't know how to fix!
        var field = fieldValuePair[0].ToLower();
        var value = fieldValuePair[1];

        switch (field)
        {
            case "url":
                _serverUrl = value;
                break;
            case "authcode":
                _authcode = value;
                break;
            default: throw new InvalidOperationException($"{field} Value is unknown in connection settings");
        }
    }
}

问题是该代码在所有实例中均可工作,除非authcode中带有=。

因此,此连接字符串未提供正确的详细信息。

<add name="ServerAuthCodeString" connectionString="url=https://devunifiedinterface.appstore.com; authCode=mQLw/OWghN0s4jQhBso7o68/KGsLnzwlWux2cnv5QYu=" />

我得到的输出是

_authcode = mQLw/OWghN0s4jQhBso7o68/KGsLnzwlWux2cnv5QYu //Missing = at the end

我需要的是

_authcode = mQLw/OWghN0s4jQhBso7o68/KGsLnzwlWux2cnv5QYu=

是的,有一个question讨论了连接字符串。 但是,答案似乎仍然无法解决带有=符号的问题。

例如,此comment专门讨论了它。

SqlConnectionStringBuilder对我不起作用。如果我传入connectionString,它会抱怨关键字url不合法。

1 个答案:

答案 0 :(得分:3)

您需要指定要从class Car(): name = "" def __init__ (self, name): self.name = name print (f"Hello and welcome {name}. You have won a Ferrari. Please enter the details below. \n") phone = input("Your phone number: ") address = input("Your home address: ") def sorry(self): print("Hey! We are sorry, we do not ship beyond 2000 Miles.") response = input("Do you still want it? Yes or No? : ").upper() if response == "YES": print(f"Cool! %s. Ride to us." % self.name) elif response == "NO": print(f"Oops! %s. We are sorry to see you go. :(" % self.name) else: print("Have a Good day!") 函数返回的拆分字符串,以及您的情况下的Split返回的值。

2

所以这会给你,

  • 索引为var fieldValuePair = component.Trim().Split(new char[] { '=' }, 2); 的第一个分割字符串:0
  • 索引为_authcode的第二个分割字符串:1(包括mQLw/OWghN0s4jQhBso7o68/KGsLnzwlWux2cnv5QYu=
相关问题