String Operators

 <  Day Day Up  >  

The string concatenation operator ( & ) takes two string operands and combines them into a single string. If either operand is not a string (or if they both are not strings), it will be converted to a string without requiring an explicit conversion, even if Option Strict is on. The result of a concatenation operation is a new string that represents the concatenation of the two operands.

Style

The + operator can also be used to concatenate strings. However, because of the possible confusion between concatenating strings ("1" + "2" = "12") and adding numbers (1 + "2" = 3), the & operator should always be used to concatenate strings.


One thing to remember is that because the concatenation operator returns an entirely new string, a series of concatenation operations may allocate quite a few new strings, which may have performance implications. When you are doing many concatenations, the type System.Text.StringBuilder can be used instead to make the concatenation faster.

 Dim Count As Integer Dim s As String Dim sb As New Text.StringBuilder() For Count = 1 To 10000   s &= CStr(Count) & "," Next Count Console.WriteLine(s) For Count = 1 To 10000   sb.Append(CStr(Count))   sb.Append(",") Next Count Console.WriteLine(s.ToString()) 

In this example, the second loop will be several times faster than the first.

The Like operator determines whether a string matches a given pattern. The first operand is the string being matched; the second operand is a string representing the pattern to match against. In the simplest case, where the pattern contains no special matching characters , the Like operator is equivalent to the = operator. However, the following character sequences have special meanings when used in a pattern.

  • The character ? matches any character.

  • The character * matches zero or more characters.

  • The character # matches any numeric digit (0 “9).

  • A list of characters delimited by brackets ( [ab...] ) matches any character in the list.

  • A list of character delimited by brackets and prefixed by an exclamation point ( [!ab...] ) matches any character not in the character list.

Two characters in a character list separated by a hyphen ( - ) specify a range of Unicode characters, starting with the first character and ending with the second character. If the second character is not later in the current thread's culture's sort order than the first character, a runtime error occurs. A hyphen that appears at the beginning or end of a character list specifies itself.

To match the special characters left bracket ( [ ), question mark ( ? ), pound ( # ), and asterisk ( * ), the particular character must be enclosed in brackets. The right bracket ( ] ) cannot be used within a group to match itself, but it can be used outside a group as an individual character. The character sequence [] is considered to be the string literal "" .

Some examples of the Like operator are the following.

 Dim r As Boolean r = "abc" Like "a?c"     ' Result: True r = "abcdef" Like "a*f"  ' Result: True r = "abc" Like "a[A-Z]c" ' Result: False r = "aBc" Like "a[A-Z]c" ' Result: True 

Character comparisons and ordering for character lists are dependent on the type of string comparisons being used. If binary comparisons are being used (i.e., Option Compare Binary ), character comparisons and ordering are based on the numeric Unicode values. If text comparisons are being used (i.e., Option Compare Text ), character comparisons and ordering are based on the current thread's culture.

Advanced

In some cultures, one character may represent two separate characters, and vice versa. For example, the character " " can be used to represent the characters "a" and "e" when they appear together, while the characters "^" and "O" can be used to represent the character " ”". When text comparisons are used, the Like operator recognizes such cultural equivalences , and an occurrence of a single character in either a pattern or a string matches the equivalent two-character sequence in the other string. Similarly, a single character in a pattern enclosed in brackets (by itself, in a list, or in a range) matches the equivalent two-character sequence in the string, and vice versa.


 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net