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
  • 28,671
  • 6
  • 55
  • 88

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
  • 28,671
  • 6
  • 55
  • 88
2

Try this

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

Or just

Guid.Parse(followActivity.Object.Replace("Board-",""));
Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68
1

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

Guid.Parse(followActivity.Object.Split(new char[]{'-'}, 2)[1]));
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
ikkentim
  • 1,639
  • 15
  • 30