Compare() 方法用于比较两个字符串。
用法:
Function Compare(ByVal str1 as String, ByVal str2 as String) as integer
参数:
- Str1:要比较的字符串。
- Str2:要比较的字符串。
返回值:它返回一个整数值,如果两个字符串都匹配,则返回 0,否则返回一个非零值。
程序/源代码:
下面给出了演示 String 类的 Compare() 方法的源代码。给定的程序已成功编译并执行。
'VB.NET program to demonstrate the Compare()
'method of String class.
Imports System
Module Module1
Sub Main()
Dim str1 As String = "Hello"
Dim str2 As String = "hello"
Dim str3 As String = "Hello"
Dim ret As Integer = 0
ret = String.Compare(str1, str2)
If ret = 0 Then
Console.WriteLine("Strings matched")
Else
Console.WriteLine("Strings are different")
End If
ret = String.Compare(str1, str3)
If ret = 0 Then
Console.WriteLine("Strings matched")
Else
Console.WriteLine("Strings are different")
End If
Console.WriteLine()
End Sub
End Module
输出:
Strings are different Strings matched Press any key to continue . . .
说明:
在上面的程序中,我们创建了一个包含 Main() 函数的模块 Module1。 Main() 函数是程序的入口点。
在 Main() 函数中,我们创建了三个字符串 str1、str2 和 str3。在这里,我们使用 String 类的 Compare() 方法比较了字符串的值。当指定字符串的值匹配时,此方法返回 0,否则将返回非零值。在这里,我们根据控制台屏幕上的字符串值打印了适当的消息。
相关用法
- VB.NET String Concat()用法及代码示例
- VB.NET String Join()用法及代码示例
- VB.NET String IsNullorWhitSpace()用法及代码示例
- VB.NET String Intern()用法及代码示例
- VB.NET String Equals()用法及代码示例
- 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 Compare() method of String class。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。