【问题标题】:How to get sentence from a string C# string manipulation如何从字符串中获取句子 C# 字符串操作
【发布时间】:2020-02-13 10:31:52
【问题描述】:

我正在尝试从字符串中获取更多信息

string input= "status":404,"userMessage":"ERROR CODE: EBE04005 | SEVERITY: E | SOURCE IDENTIFIER: EBE04 | DESCRIPTION: The User Profile Retrieval Service was unable to process due to an unavailable Data Source: | Additional Info: Prodcucer Detail Not Found In Producer DB For Producer Code: 123456"

想要的输出是:

在生产者数据库中找不到生产者详细信息,生产者代码:123456

为此我正在尝试

string input = input.Substring(input.IndexOf("Info:"))

但没有得到想要的结果。下面是输出

信息:在生产者数据库中找不到生产者详细信息,生产者代码:123456

【问题讨论】:

  • 预期输出是什么?
  • 这个字符串是 JSON。使用JSON.NET 并将其反序列化为对象并读取属性。
  • @BWA 它不是一个有效的 JSON ......即使......他想要来自 userMessage 属性的一部分
  • @BWA 这不是 JSON。即使是这样,信息也将是 inside 一个字符串属性。 JSON 解析无济于事

标签: c# .net


【解决方案1】:

您发布的代码将使用Info 标记的开头,因此您需要添加一点。

string input = input.Substring(input.IndexOf("Info:") + "Info:".Length)

【讨论】:

    【解决方案2】:

    如果你的单词不在字符串中,你应该处理。

    string toBeSearched = "Info:";
    int io= myString.IndexOf(toBeSearched);
    
    if (io!= -1) 
    {
        string code = myString.Substring(io+ toBeSearched.Length);
        // do something here
    }
    

    【讨论】:

      【解决方案3】:

      您可以为此目的使用Regex。例如,

      var match = Regex.Match(input,@"Additional Info:\s*(?<AdditionalInfo>[^\""]*)");
      if(match.Success)
      {
        var additionalInfo = match.Groups["AdditionalInfo"].Value;
      }
      

      在上面的代码中,Regex.Match 方法在输入字符串中搜索指定的 Regex 表达式。在找到匹配项时,使用命名组来提取 AdditionalInfo

      输出

      Prodcucer Detail Not Found In Producer DB For Producer Code: 123456
      

      【讨论】:

        【解决方案4】:

        现场演示:https://dotnetfiddle.net/m0qUbq 由于感兴趣的字符串的位置在输入字符串的最后面,推荐使用LastIndexOf

        string input= "... Data Source: | Additional Info: Prodcucer Detail Not Found In Producer DB For Producer Code: 123456";
        input.Substring(input.LastIndexOf("Info: ") + "Info: ".Length)  
        

        【讨论】:

          【解决方案5】:

          如果您的字符串是 JSON,则在您的示例中缺少开头的“{”,但我认为您在应用程序中有有效的 JSON。

          此代码从您的消息中获取所有信息:

          using System;
          using System.Collections.Generic;
          using Json.Net;
          
          namespace ConsoleApp
          {
              internal class Program
              {
                  public static void Main(string[] args)
                  {
                      string input= "{\"status\":404,\"userMessage\":\"ERROR CODE: EBE04005 | SEVERITY: E | SOURCE IDENTIFIER: EBE04 | DESCRIPTION: The User Profile Retrieval Service was unable to process due to an unavailable Data Source: | Additional Info: Prodcucer Detail Not Found In Producer DB For Producer Code: 123456\"}";
          
                      Data data = JsonNet.Deserialize<Data>(input);
          
                      string[] messages = data.userMessage.Split('|');
          
                      Dictionary<string, string> messageDict = new Dictionary<string, string>();
          
                      foreach (string message in messages)
                      {
                          string[] tmp = message.Split(':');
                          messageDict.Add(tmp[0].Trim(), tmp[1].Trim());
                      }
          
                      foreach (string key in messageDict.Keys)
                      {
                          Console.WriteLine($"Key: {key} Value: {messageDict[key]}");
                      }
                  }
              }
          
              public class Data
              {
                  public int status { get; set; }
          
                  public string userMessage { get; set; }
              }
          }
          

          然后输出:

          Key: ERROR CODE Value: EBE04005
          Key: SEVERITY Value: E
          Key: SOURCE IDENTIFIER Value: EBE04
          Key: DESCRIPTION Value: The User Profile Retrieval Service was unable to process due to an unavailable Data Source
          Key: Additional Info Value: Prodcucer Detail Not Found In Producer DB For Producer Code
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-25
            • 1970-01-01
            • 1970-01-01
            • 2012-01-26
            相关资源
            最近更新 更多