Geeks With Blogs

News FAQ on the correct forum to post at: http://forums.asp.net/p/1337412/2699239.aspx#2699239

Tatworth

Recently I discovered that there were some formatting options when casting a GUID to a string.

So here is sample code:

      Console.WriteLine("Guid.NewGuid().ToString()" + Guid.NewGuid().ToString());
      Console.WriteLine("Guid.NewGuid().ToString(\"N\")" + Guid.NewGuid().ToString("N"));
      Console.WriteLine("Guid.NewGuid().ToString(\"D\")" + Guid.NewGuid().ToString("D"));
      Console.WriteLine("Guid.NewGuid().ToString(\"B\")" + Guid.NewGuid().ToString("B"));
      Console.WriteLine("Guid.NewGuid().ToString(\"P\")" + Guid.NewGuid().ToString("P"));
      Console.WriteLine("Guid.NewGuid().ToString(\"X\")" + Guid.NewGuid().ToString("X"));

Here is the output from one test run.

Guid.NewGuid().ToString()ba3b5308-b556-49b3-b439-8b5e7a6d3d64
Guid.NewGuid().ToString("N")b3d3d49c60e0426a8e4ff3fabff1c4e9
Guid.NewGuid().ToString("D")b7bfbbf8-feb0-4f5e-9016-31b2d8219e9c
Guid.NewGuid().ToString("B"){8964d6f0-6630-4ea1-8826-7c9eab049642}
Guid.NewGuid().ToString("P")(971bdacc-4dfe-4c6f-98c3-27dfeafb1cbe)
Guid.NewGuid().ToString("X"){0x4d39b209,0x8019,0x4fa7,{0xaa,0x7f,0x47,0xc9,0x87,0xe9,0x1c,0x9f}}

These are some useful options!

For more details see http://msdn.microsoft.com/en-us/library/97af8hh4.aspx

Posted on Sunday, May 8, 2011 5:31 PM | Back to top


Comments on this post: Formatting GUID strings

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © TATWORTH | Powered by: GeeksWithBlogs.net