比较字符串:
Module strings
Sub Main()
Dim str1, str2 As String
str1 = "This is test"
str2 = "This is text"
If (String.Compare(str1, str2) = 0) Then
Console.WriteLine(str1 + " and " + str2 +
" are equal.")
Console.WriteLine(str1 + " and " + str2 +
" are not equal.")
End If
Console.ReadLine()
End Sub
End Module
当上面的代码被编译并执行时,会产生以下结果:
This is test and This is text are not equal.
字符串包含字符串:
Module strings
Sub Main()
Dim str1 As String
str1 = "This is test"
If (str1.Contains("test")) Then
Console.WriteLine("The sequence 'test' was found.")
End If
Console.ReadLine()
End Sub
End Module
当上面的代码被编译并执行时,会产生以下结果:
The sequence 'test' was found.
获取子字符串:
Module strings
Sub Main()
Dim str As String
str = "Last night I dreamt of San Pedro"
Console.WriteLine(str)
Dim substr As String = str.Substring(23)
Console.WriteLine(substr)
Console.ReadLine()
End Sub
End Module
当上面的代码被编译并执行时,会产生以下结果:
Last night I dreamt of San Pedro San Pedro.
连接字符串:
Module strings
Sub Main()
Dim strarray As String() = {"Down the way where the nights are gay",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"}
Dim str As String = String.Join(vbCrLf, strarray)
Console.WriteLine(str)
Console.ReadLine()
End Sub
End Module
当上面的代码被编译并执行时,会产生以下结果:
Down the way where the nights are gay
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop