本文整理匯總了VB.NET中System.String.Substring方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET String.Substring方法的具體用法?VB.NET String.Substring怎麽用?VB.NET String.Substring使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.String
的用法示例。
在下文中一共展示了String.Substring方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: SubStringTest
Public Class SubStringTest
Public Shared Sub Main()
Dim info As String() = { "Name: Felica Walker", "Title: Mz.",
"Age: 47", "Location: Paris", "Gender: F"}
Dim found As Integer = 0
Console.WriteLine("The initial values in the array are:")
For Each s As String In info
Console.WriteLine(s)
Next s
Console.WriteLine(vbCrLf + "We want to retrieve only the key information. That is:")
For Each s As String In info
found = s.IndexOf(": ")
Console.WriteLine(" {0}", s.Substring(found + 2))
Next s
End Sub
End Class
輸出:
The initial values in the array are: Name: Felica Walker Title: Mz. Age: 47 Location: Paris Gender: F We want to retrieve only the key information. That is: Felica Walker Mz. 47 Paris F
示例2: Example
Module Example
Public Sub Main()
Dim value As String = "This is a string."
Dim startIndex As Integer = 5
Dim length As Integer = 2
Dim substring As String = value.Substring(startIndex, length)
Console.WriteLine(substring)
End Sub
End Module
輸出:
is
示例3: Example
Module Example
Public Sub Main()
Dim pairs() As String = { "Color1=red", "Color2=green", "Color3=blue",
"Title=Code Repository" }
For Each pair In pairs
Dim position As Integer = pair.IndexOf("=")
If position < 0 then Continue For
Console.WriteLine("Key: {0}, Value: '{1}'",
pair.Substring(0, position),
pair.Substring(position + 1))
Next
End Sub
End Module
輸出:
Key: Color1, Value: 'red' Key: Color2, Value: 'green' Key: Color3, Value: 'blue' Key: Title, Value: 'Code Repository'
示例4: Sample
Public Class Sample
Public Shared Sub Main()
Dim myString As String = "abc"
Dim test1 As Boolean = myString.Substring(2, 1).Equals("c") ' This is true.
Console.WriteLine(test1)
Dim test2 As Boolean = String.IsNullOrEmpty(myString.Substring(3, 0)) ' This is true.
Console.WriteLine(test2)
Try
Dim str3 As String = myString.Substring(3, 1) ' This throws ArgumentOutOfRangeException.
Console.WriteLine(str3)
Catch e As ArgumentOutOfRangeException
Console.WriteLIne(e.Message)
End Try
End Sub
End Class
輸出:
True True Index and length must refer to a location within the string. Parameter name: length
示例5: Example
Module Example
Public Sub Main()
Dim s As String = "aaaaabbbcccccccdd"
Dim charRange As Char = "b"c
Dim startIndex As Integer = s.Indexof(charRange)
Dim endIndex As Integer = s.LastIndexOf(charRange)
Dim length = endIndex - startIndex + 1
Console.WriteLine("{0}.Substring({1}, {2}) = {3}",
s, startIndex, length,
s.Substring(startIndex, length))
End Sub
End Module
輸出:
aaaaabbbcccccccdd.Substring(5, 3) = bbb
示例6: Example
Module Example
Public Sub Main()
Dim s As String = "<term>extant<definition>still in existence</definition></term>"
Dim searchString As String = "<definition>"
Dim startindex As Integer = s.IndexOf(searchString)
searchString = "</" + searchString.Substring(1)
Dim endIndex As Integer = s.IndexOf(searchString)
Dim substring As String = s.Substring(startIndex, endIndex + searchString.Length - StartIndex)
Console.WriteLine("Original string: {0}", s)
Console.WriteLine("Substring; {0}", substring)
End Sub
End Module
輸出:
Original string:extant Substring;still in existence still in existence
示例7: String.Substring(Int begin)
Public Class MainClass
Public Shared Sub Main()
'Strings
Dim strConstant As String = "ABC"
Dim strRepeat As New String("A"c, 20)
Dim strMyString As String = "Hello World"
Console.WriteLine(strMyString.Substring(0, 5))
Console.WriteLine(strMyString.Substring(6))
End Sub
End Class