In: Computer Science
Visual Studio Basic
1: Which string function you will be likely using to search for
a certain pattern in a string?
Describe your answer.
2: If you are to find the occurrence of 4-letter string “abcd”
regardless of its case combinations,
such as “AbCd”, “ABCd” and etc, in a long string, what would you
do? Describe your answer.
Strings.StrComp(String, String, CompareMethod) Method
Returns -1, 0, or 1, based on the result of a string comparison.
C#
public static int StrComp (string String1, string String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
Parameters
String1
String
Required. Any valid String
expression.
String2
String
Required. Any valid String
expression.
Compare
CompareMethod
Optional. Specifies the type of string comparison. If
Compare
is omitted, the Option Compare
setting determines the type of comparison.
Returns
Int32
The StrComp
function has the following return
values.
If | StrComp returns |
String1 sorts ahead of String2 |
-1 |
String1 is equal to String2 |
0 |
String1 sorts after String2 |
1 |
Exceptions
ArgumentException
Compare
value is not valid.
Examples
This example uses the StrComp
function to return
the results of a string comparison.
VB
' Defines variables. Dim testStr1 As String = "ABCD" Dim testStr2 As String = "abcd" Dim testComp As Integer ' The two strings sort equally. Returns 0. testComp = StrComp(testStr1, testStr2, CompareMethod.Text) ' testStr1 sorts before testStr2. Returns -1. testComp = StrComp(testStr1, testStr2, CompareMethod.Binary) ' testStr2 sorts after testStr1. Returns 1. testComp = StrComp(testStr2, testStr1, CompareMethod.Binary)
Remarks
The strings are compared by alphanumeric sort values beginning with the first character. For further information on binary comparisons, textual comparisons, and sort order, see Option Compare Statement.
The Compare
argument settings are:
Constant | Description |
---|---|
Binary |
Performs a binary comparison, based on a sort order derived from the internal binary representations of the characters. |
Text |
Performs a text comparison, based on a case-insensitive text
sort order determined by your system's LocaleID
value. |