0

I have a string in this format:

"object": "Board-f330c10a-a9eb-4253-b554-43ed95c87242"

and I want to extract guid from it.I was trying like this:

Guid.Parse(followActivity.Object.Split('-').Skip(1).FirstOrDefault());

but it takes only the first part of the guid string. How can I extract whole guid?

Can anybody help.

sujith karivelil
  • 27,818
  • 6
  • 51
  • 82

4 Answers4

4

Try something like this Example , if every input follows the same patter like Board-:

string complexGuid = "Board-f330c10a-a9eb-4253-b554-43ed95c87242";
string extractedGuid = complexGuid.Substring(complexGuid.IndexOf('-') +1 );

Here complexGuid.IndexOf('-') will return the first index of the '-' which is the - after the Board in the given example. we need to skip that also so add a +1 so that .Substring() will give you the expected output for you.

sujith karivelil
  • 27,818
  • 6
  • 51
  • 82
2

Try this

 Guid.Parse(string.Join("-",followActivity.Object.Split('-').Skip(1)))
mukesh kudi
  • 709
  • 6
  • 20
2

Or just

Guid.Parse(followActivity.Object.Replace("Board-",""));
Captain Kenpachi
  • 6,778
  • 6
  • 46
  • 66
1

If the format is always the same, you could use string.Split()

Guid.Parse(followActivity.Object.Split(new char[]{'-'}, 2)[1]));
sujith karivelil
  • 27,818
  • 6
  • 51
  • 82
ikkentim
  • 1,581
  • 12
  • 29