参考:https://msdn.microsoft.com/en-us/library/97af8hh4.aspx
测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GuidToString
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " -->" + System.Guid.NewGuid().ToString());
Console.WriteLine( "N-->" + System.Guid.NewGuid().ToString( "N" ));
Console.WriteLine( "D-->" + System.Guid.NewGuid().ToString( "D" ));
Console.WriteLine( "B-->" + System.Guid.NewGuid().ToString( "B" ));
Console.WriteLine( "P-->" + System.Guid.NewGuid().ToString( "P" ));
Console.WriteLine( "X-->" + System.Guid.NewGuid().ToString( "X" ));
Console.ReadKey();
}
}
}
|
测试结果:
注意事项:只能使用N、D、B、P、X(不区分大小写)空,使用其他字母会出现异常
格式字符串只能是“D”、“d”、“N”、“n”、“P”、“p”、“B”、“b”、“X”或“x”。
异常情况:
以下都正常:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GuidToString
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine( " -->" + System.Guid.NewGuid().ToString());
Console.WriteLine( " -->" + System.Guid.NewGuid().ToString( "" ));
Console.WriteLine( "N-->" + System.Guid.NewGuid().ToString( "N" ));
Console.WriteLine( "n-->" + System.Guid.NewGuid().ToString( "n" ));
Console.WriteLine( "D-->" + System.Guid.NewGuid().ToString( "D" ));
Console.WriteLine( "d-->" + System.Guid.NewGuid().ToString( "d" ));
Console.WriteLine( "B-->" + System.Guid.NewGuid().ToString( "B" ));
Console.WriteLine( "b-->" + System.Guid.NewGuid().ToString( "b" ));
Console.WriteLine( "P-->" + System.Guid.NewGuid().ToString( "P" ));
Console.WriteLine( "p-->" + System.Guid.NewGuid().ToString( "p" ));
Console.WriteLine( "X-->" + System.Guid.NewGuid().ToString( "X" ));
Console.WriteLine( "x-->" + System.Guid.NewGuid().ToString( "x" ));
Console.ReadKey();
}
}
}
|
Guid.NewGuid().ToString()的几种格式
1、Guid.NewGuid().ToString("N") 结果为:
38bddf48f43c48588e0d78761eaa1ce6
2、Guid.NewGuid().ToString("D") 结果为:
57d99d89-caab-482a-a0e9-a0a803eed3ba
3、Guid.NewGuid().ToString("B") 结果为:
{09f140d5-af72-44ba-a763-c861304b46f8}
4、Guid.NewGuid().ToString("P") 结果为:
(778406c2-efff-4262-ab03-70a77d09c2b5)