Home > database >  How to get a string from an array with specified starting character of the string and my string is J
How to get a string from an array with specified starting character of the string and my string is J

Time:11-10

I have list of string array which having string type and the result should be the string a serialized JSON string in an array.

The below code not works.

string[] names = { "Rob", "", "Robert", 
"{\"Id\":\"01GHEAPQ180YC4MWX8YRJ4JXDY\",\"Name\":\"Robert\"}", "Bob" };

IEnumerable<string> query =
    names.TakeWhile(name => name.Contains("}"));

foreach (string json in query)
{
    Console.WriteLine(json);
}

expected output
o/p : "{\"Id\":\"01GHEAPQ180YC4MWX8YRJ4JXDY\",\"Name\":\"Robert\"}"

CodePudding user response:

Instead of TakeWhile use Where

IEnumerable<string> query = names.Where(name => name.Contains("}"));

test

foreach (string json in query)
{
    Console.WriteLine(json);
}

{"Id":"01GHEAPQ180YC4MWX8YRJ4JXDY","Name":"Robert"}
  • Related