Equals() 方法用於檢查兩個字符串是否相等並返回一個布爾值。
用法:
Function Equals(ByVal str1 as String, ByVal str2 as String) as Boolean
參數:
- Str1:要比較的字符串。
- Str2:要比較的字符串。
返回值:它返回一個布爾值,如果兩個字符串相等則返回真,否則返回假。
程序/源代碼:
下麵給出了演示 String 類的 Equals() 方法的源代碼。給定的程序已成功編譯並執行。
'VB.NET program to demonstrate the Equals() method
'of String class.
Imports System
Module Module1
Sub Main()
Dim str1 As String = "hello World"
Dim str2 As String = "Hello World"
Dim str3 As String = "Hello World"
Dim ret As Boolean
ret = String.Equals(str1, str2)
If ret = True Then
Console.WriteLine("Strings str1 and str2 are equal")
Else
Console.WriteLine("Strings str1 and str2 are not equal")
End If
ret = String.Equals(str2, str3)
If ret = True Then
Console.WriteLine("Strings str2 and str3 are equal")
Else
Console.WriteLine("Strings str2 and str3 are not equal")
End If
End Sub
End Module
輸出:
Strings str1 and str2 are not equal Strings str2 and str3 are equal Press any key to continue . . .
說明:
在上麵的程序中,我們創建了一個包含 Main() 函數的模塊 Module1。 Main() 函數是程序的入口點。
在 Main() 函數中,我們創建了三個字符串 str1、str2 和 str3。在這裏,我們使用 String 類的 Equals() 方法比較了字符串的值。當指定字符串的值相等時,此方法返回真,否則將返回假值。在這裏,我們根據控製台屏幕上的字符串值打印了適當的消息。
相關用法
- VB.NET String Join()用法及代碼示例
- VB.NET String IsNullorWhitSpace()用法及代碼示例
- VB.NET String Compare()用法及代碼示例
- VB.NET String Intern()用法及代碼示例
- VB.NET String Concat()用法及代碼示例
- VB.NET String IsNullorEmpty()用法及代碼示例
- VB.NET LTrim()用法及代碼示例
- VB.NET DateTime FromBinary()用法及代碼示例
- VB.NET CLng()用法及代碼示例
- VB.NET RTrim()用法及代碼示例
- VB.NET GetChar()用法及代碼示例
- VB.NET Array Reverse()用法及代碼示例
- VB.NET DateTime ToBinary()用法及代碼示例
- VB.NET Array Resize()用法及代碼示例
- VB.NET CInt()用法及代碼示例
- VB.NET CULng()用法及代碼示例
- VB.NET CDbl()用法及代碼示例
- VB.NET LCase()用法及代碼示例
- VB.NET Array ConstrainedCopy()用法及代碼示例
- VB.NET Array Clear()用法及代碼示例
注:本文由純淨天空篩選整理自 VB.Net program to demonstrate the Equals() method of String class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。