Red*_*ger 1 vb.net basic visual-studio-2012
我想在 Visual Basic 中的特定位置获得一个可用的字符,例如字符串是“APPLE”。
我想获取字符串中的第三个字符,即“P”。
您可以将字符串视为字符数组。字符的索引从 0 到字符数减 1。
' For the 3rd character (the second P):
Dim s As String = "APPLE"
Dim ch As Char = s(2) ' = 'P', where s(0) is "A"
Run Code Online (Sandbox Code Playgroud)
或者
Dim ch2 As Char = s.Chars(2) 'According to @schlebe's comment
Run Code Online (Sandbox Code Playgroud)
或者
Dim substr As String = s.Substring(2, 1) 's.Substring(0, 1) is "A"
Run Code Online (Sandbox Code Playgroud)
或者
Dim substr As String = Mid(s, 3, 1) 'Mid(s, 1, 1) is "A" (this is a relict from VB6)
Run Code Online (Sandbox Code Playgroud)
注意:如果你想返回一个Char. 其他两个返回String长度为 1 的a 。在所有语言中可用的通用 .NET 方式是使用 method Substring,因为该函数Mid是特定于 VB 的,并且是为了促进从 VB6 到 VB.NET 的过渡而引入的。
| 归档时间: |
|
| 查看次数: |
30978 次 |
| 最近记录: |