vb.net String.Format格式说明符汇总
时间: 2024-01-16 17:03:10 浏览: 42
在 VB.NET 中,String.Format 方法支持多种格式说明符,用于指定数字、日期和字符串的格式化方式。下面是一些常用的格式说明符:
#### 数字格式说明符
格式说明符 | 描述
--- | ---
C | 货币。例如:$1,234.56
D | 十进制。例如:1234
E | 科学计数法。例如:1.234500E+003
F | 固定点。例如:1234.56
G | 通用格式。例如:12.345(小数点后位数不确定时使用)
N | 数字。例如:1,234.56
P | 百分比。例如:12.35%
以下是一些示例:
```
Dim num As Double = 1234.5678
Dim intNum As Integer = 1234
Dim message1 As String = String.Format("Currency: {0:C}", num) ' $1,234.57
Dim message2 As String = String.Format("Decimal: {0:D}", intNum) ' 1234
Dim message3 As String = String.Format("Scientific: {0:E}", num) ' 1.234568E+003
Dim message4 As String = String.Format("Fixed-point: {0:F2}", num) ' 1234.57
Dim message5 As String = String.Format("General: {0:G}", num) ' 1234.5678
Dim message6 As String = String.Format("Number: {0:N}", num) ' 1,234.57
Dim message7 As String = String.Format("Percentage: {0:P}", num) ' 1234.57%
```
#### 日期和时间格式说明符
格式说明符 | 描述
--- | ---
d | 短日期。例如:6/15/2009
D | 长日期。例如:June 15, 2009
t | 短时间。例如:3:45 PM
T | 长时间。例如:3:45:30 PM
f | 完整日期和时间(长日期 + 短时间)。例如:June 15, 2009 3:45 PM
F | 完整日期和时间(长日期 + 长时间)。例如:June 15, 2009 3:45:30 PM
g | 通用日期和时间(短日期 + 短时间)。例如:6/15/2009 3:45 PM
G | 通用日期和时间(短日期 + 长时间)。例如:6/15/2009 3:45:30 PM
M | 月日格式。例如:June 15
R | RFC1123 格式。例如:Mon, 15 Jun 2009 20:45:30 GMT
s | 可排序格式。例如:2009-06-15T15:45:30
u | 通用可排序格式。例如:2009-06-15 20:45:30Z
U | 以通用时间(UTC)为基准的长日期时间格式。例如:Monday, June 15, 2009 8:45:30 PM
Y | 年月格式。例如:June, 2009
以下是一些示例:
```
Dim today As DateTime = DateTime.Today
Dim now As DateTime = DateTime.Now
Dim message1 As String = String.Format("Short date: {0:d}", today) ' 6/15/2009
Dim message2 As String = String.Format("Long date: {0:D}", today) ' June 15, 2009
Dim message3 As String = String.Format("Short time: {0:t}", now) ' 3:45 PM
Dim message4 As String = String.Format("Long time: {0:T}", now) ' 3:45:30 PM
Dim message5 As String = String.Format("Full date/time (short): {0:f}", now) ' June 15, 2009 3:45 PM
Dim message6 As String = String.Format("Full date/time (long): {0:F}", now) ' June 15, 2009 3:45:30 PM
Dim message7 As String = String.Format("General date/time (short): {0:g}", now) ' 6/15/2009 3:45 PM
Dim message8 As String = String.Format("General date/time (long): {0:G}", now) ' 6/15/2009 3:45:30 PM
```
#### 字符串格式说明符
格式说明符 | 描述
--- | ---
s | 字符串。例如:"Hello, world!"
x | 十六进制。例如:"48656C6C6F20776F726C6421"("Hello world!" 的 ASCII 码)
以下是一些示例:
```
Dim str As String = "Hello, world!"
Dim message1 As String = String.Format("String: {0}", str) ' Hello, world!
Dim message2 As String = String.Format("Hexadecimal: {0:X}", str) ' 48656C6C6F20776F726C6421
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)