查找并用映射值替换子字符串

时间:2019-04-24 12:51:21

标签: c#

某些设置存储在字符串中。一个例子可能是

string rawWatermarkFormat = "{@Charge}; {Current Date} {Current Time}; Scan Operator: {Scan Operator's User ID}; Page: x/y";

每个由 {...} 包裹的子字符串代表字典的键。该词典包含需要替换密钥的值。最后的字符串将是

string parsedWatermarkFormat = "phosphoric acid; 24.04.2019 14:20; Scan Operator: 123; Page: x/y";

(是的,当前日期和当前时间也存储在该集合中)

我想提取这些键,从集合中获取正确的值,然后将键替换为该值。我从这里开始

    public Parse()
    {
        string watermarkFormat = "{@Charge}; {Current Date} {Current Time}; Scan Operator: {Scan Operator's User ID}; Page: x/y"; // batch.get_CustomStorageString(Resources.WATERMARK_FORMAT_KEY);

        int currentFieldStart = 0;

        for (int i = 0; i < watermarkFormat.Length; i++)
        {
            char currentCharacter = watermarkFormat[i];

            if (currentCharacter == '{')
            {
                currentFieldStart = i;
            }
            else if (currentCharacter == '}')
            {
                int length = i - currentFieldStart;
                string currentField = watermarkFormat.Substring(currentFieldStart + 1, length - 1);

                string fieldValue = "---"; // access the collection for the correct field value

                watermarkFormat = watermarkFormat
                    .Remove(currentFieldStart, length)
                    .Insert(currentFieldStart, fieldValue);
            }
        }
    }

问题是,如果我将使用范围替换子字符串,则字符串会中断。如果键的长度为x,值的长度为y,则replace方法不适用于下一个字段。

因此,此watermarkFormat示例字符串将导致

---} {Current Time}; Scan Operator: ---}; Page: x/y

如何正确替换字段?


编辑rawWatermarkFormat只是一个示例字符串!我不知道它的长度,令牌等。所以字符串也可能是这个

string rawWatermarkFormat = "{Current Date} {Current Date} {Current Date} {Current Date} {Current Date}"

或此

string rawWatermarkFormat = "{blue car}"

我必须提取这些字符串并从字典中获取正确的值。我自己不知道这些标记。

4 个答案:

答案 0 :(得分:2)

使用正则表达式作为替代。

string pattern = @"\{[a-zA-Z\-\s\@\']+\}";
string input = "{@Charge}; {Current Date} {Current Time}; Scan Operator: {Scan Operator's User ID}; Page: x/y";
var matches = System.Text.RegularExpressions.Regex.Matches(input, pattern);
foreach (var match in matches)
{
    Console.WriteLine(match);
    // TODO replace
    // var key = match.ToString();
    // input = input.Replace(key, someMethodToGetValue(key.Replace("{", "").Replace("}", "")));
}

控制台输出:

{@Charge}
{Current Date}
{Current Time}
{Scan Operator's User ID}

答案 1 :(得分:1)

使用{ "data": [ { "Date": "02/04/2019", "Total": "1000", "Success": "850", "Failure": "150", "FailureDeatils": [ { "name": "Reason1", "Count": 2, "Description": [ { "DescriptionID": "01", "Status": "Failure Point1" }, { "DescriptionID": "01", "Status": "Failure Point1" } ] }, { "name": "Reason2", "Count": 6, "Description": [ { "DescriptionID": "01", "Status": "Failure Point1" }, { "DescriptionID": "01", "Status": "Failure Point1" } ] } ] }, { "Date": "03/04/2019", "Total": "800", "Success": "750", "Failure": "150", "FailureDeatils": [ { "name": "Reason1", "Count": 3, "Description": [ { "DescriptionID": "01", "Status": "Failure Point1" }, { "DescriptionID": "01", "Status": "Failure Point1" } ] }, { "name": "Reason2", "Count": 1, "Description": [ { "DescriptionID": "01", "Status": "Failure Point1" }, { "DescriptionID": "01", "Status": "Failure Point1" } ] } ] } ] } 委托重载,可以很容易地用 var filtered = data .filter(value => data.Date == "02/04/2019") .map(e => e.FailureDeatils); 解决。

这是一个LINQPad示例:

Regex.Replace

这将输出:

MatchEvaluator

答案 2 :(得分:0)

这可以工作:

 var Charge = "phosphoric acid";
 var CurrentDate = "24.04.2019";
 var CurrentTime = "14:20";
 var ScanOperatorUserID = "123";
 var interpolated = $"{Charge}; {CurrentDate} {CurrentTime}; Scan Operator: {ScanOperatorUserID}; Page: x/y";
 string parsedWatermarkFormat = "phosphoric acid; 24.04.2019 14:20; Scan Operator: 123; Page: x/y";
 Console.WriteLine(interpolated);
 Console.WriteLine(parsedWatermarkFormat);
 Console.ReadKey();

希望这会有所帮助。

答案 3 :(得分:0)

我反过来做。获取字典并替换所有提及的键。

foreach(KeyValuePair<string,string> variable in variableDictionary )
                {
                    fileContent = fileContent.Replace("$(" + variable.Key + ")", variable.Value);
                }