1

I am trying to get additional info from the string

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"

the desired output is:

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

for this i am trying to

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

but not getting the desired result. below is the output

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

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Sam
  • 25
  • 5

5 Answers5

2

The code you posted will use the start of the Info marker, so you need to add a bit.

string input = input.Substring(input.IndexOf("Info:") + "Info:".Length)
Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
1

You should handle if your word isnt in the string.

string toBeSearched = "Info:";
int io= myString.IndexOf(toBeSearched);

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

You could use Regex for the purpose. For example,

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

In the above code, the Regex.Match method searches input string for the specified Regex Expression. On finding a match, named groups are used to extract the AdditionalInfo

Output

Prodcucer Detail Not Found In Producer DB For Producer Code: 123456
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
1

Live demo: https://dotnetfiddle.net/m0qUbq Since the location of the string of interest is in the rear most of the input string, recommended using 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)  
David
  • 15,894
  • 22
  • 55
  • 66
1

If your string is JSON, in your example is missing "{" at the beginning, but i think you have valid JSON in app.

This code gets all info from your message:

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; }
    }
}

And output:

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
BWA
  • 5,672
  • 7
  • 34
  • 45