Sie sind auf Seite 1von 8

Trim Trailing or Leading Blanks / Whitespace from a String

Use the Visual Basic functions LTrim$, RTrim$, and Trim$ to trim whitespace from a string. MyString = " Chilkat Software " Print LTrim$(MyString) ' prints "Chilkat Software " Print RTrim$(MyString) ' prints " Chilkat Software" Print Trim$(MyString) ' prints "Chilkat Software"

String Concatenation
The string operator & performs a string concatenation.

Print "Hello " & "World!"

' prints "Hello World!"

Extract Substring from the Beginning of a String


Use the Left$ to extract a substring from the beginning of a string. MyString = "1234567890" Print Left$(MyString,4) ' Prints "1234"

Extract Substring from the End of a String


Use the Right$ to extract a substring from the end of a string. MyString = "1234567890" Print Right$(MyString,4) ' Prints "7890"

Extract Substring from the Middle of a String


Use the Mid$ to extract a substring from the middle of a string. Note: Be sure to use Left$, Right$ and Mid$ instead of "Left", "Right", and "Mid" because if you omit the "$", Visual Basic returns a Variant that must be re-converted back to a string when the result is used in another string expression. MyString = "1234567890" startPos = 3 charCount = 4 Print Mid$(MyString,startPos,charCount) ' Prints "3456"

Modify Characters Within a String


You can use Mid$ to modify a substring within a string. MyString = "1234567890" startPos = 3 charCount = 4 Mid$(MyString,startPos,charCount) = "XXXX" Print MyString ' Prints "12XXXX7890"

Get Length of String


Use the Visual Basic "Len" function to return the length of a string. MyString = "1234567890" Print Len(MyString) ' Prints 10

Fixed-Length Strings
Fixed length strings are automatically filled with spaces to pad them to their fixed-length. When working with fixed-length strings, you can use RTrim$ to trim the extra spaces. (See example below.) If a fixed-length string is declared, it will be filled with Null characters until it is used. The Visual Basic trim functions (RTrim$, LTrim$, and Mid$) will not trim Null characters, so be sure to assign a fixedlength string to an empty string "" immediately. Dim MyString As String * 10 MyString = "Chilkat" ' MyString contains "Chilkat "

Print RTrim$(MyString) ' prints "Chilkat" Dim AnotherString As String * 10 Print Len(RTrim$(AnotherString)) ' Prints 10, because the string contains Null characters.

Return ASCII Character Code of First Letter in String


The Visual Basic Asc function returns the ASCII code of the first character in the string. Print Asc("ABCD") ' prints 65

Convert ASCII Value to Character


The Visual Basic Chr function takes an ASCII character code and returns the character. Print Chr(65) ' prints "A"

Create String with Repeating Characters


Use the Space$ and String$ to create a string having a single character repeated N times. Dim fiveSpaces as String fiveSpaces = Space$(5) Dim tenDots as String tenDots = String$(10,".")

Case-Insensitive String Compare


Use the StrComp function to compare strings without case sensitivity. string1 = "Alaska" string2 = "California" result = StrComp(string1,string2,vbTextCompare) ' result = 0 if the strings are identical (case-insensitive) ' result = -1 if string1 is lexicographically less than string2 ' result = 1 if string1 is lexicographically greater than string2

Convert String to Lowercase


Use the LCase$ function to convert a string to lowercase. company = "Chilkat Software" Print LCase$(company) ' prints "chilkat software"

Convert String to Uppercase


Use the UCase$ function to convert a string to uppercase. company = "Chilkat Software" Print UCase$(company) ' prints "CHILKAT SOFTWARE"

Convert String to Proper Case


Use the StrConv function to convert a string to proper case. company = "CHILKAT SOFTWARE" Print StrConv(company,vbProperCase) ' prints "Chilkat Software"

Convert String to ANSI Byte Array


Use the StrConv function with "vbFromUnicode" to convert a Visual Basic string to a byte array of ANSI characters. Dim company As String Dim b() As Byte company = "CHILKAT SOFTWARE" b = StrConv(company,vbFromUnicode) ' We now have a byte array containing 'C', 'H', 'I', 'L', ...

Convert Byte Array to String


Use the StrConv function with "vbUnicode" to convert a byte array of ANSI characters to a string. Dim s As String Dim b(1 To 3) As Byte b(1) = Asc("A") b(2) = Asc("B") b(3) = Asc("C") s = StrConv(b, vbUnicode) ' We now have a string containing "ABC"

Copy Unicode String to Byte Array


This example copies a Unicode string to a byte array, with no conversion. Dim s As String s = "ABC" Dim b(1 To 6) As Byte For i = 1 To 6 b(i) = AscB(MidB(s, i)) Next ' We now have a byte array containing 65, 0, 66, 0, 67, 0

Convert a Number to a String


Use Str$ or CStr to convert a number into it's string representation. The difference between Str$ and CStr is that Str$ will add a leading space if the number is positive. Dim v As Integer v = 100 Print Str$(v) Print CStr(v) ' Prints " 100" ' Prints "100"

Convert a String to Currency


Use CCur to convert a string to the Currency type. CCur is locale aware. Dim s As String s = "$120.15" Dim cc As Currency cc = CCur(s) Print cc ' prints "120.15"

Determine if a String Contains a Valid Date


Use IsDate to determine if a string contains a valid date. Dim d1, d2, d3 d1 = "August 12, 2004" d2 = "12/25/2000" d3 = "Chilkat Software" result = IsDate(d1) ' Returns True. result = IsDate(d2) ' Returns True. result = IsDate(d3) ' Returns False.

Convert a String to a Date / Time


Use CDate to convert a string to a date/time. Dim d1 As Date Dim d2 As Date Dim d3 As Date d1 = CDate("August 12, 2004") d2 = CDate("2:07:30 PM") d3 = CDate("August 12, 2004 2:07:30 PM") Print d1 ' prints 8/12/2004 Print d2 ' prints 2:07:30 PM Print d3 ' prints 8/12/2004 2:07:30 PM

Convert a String to a Double


Converting a string to a double is simple: Dim d1 As Double Dim d2 As Double ' You can be explicit d1 = CDbl("12.2") ' Or you can just do this... d2 = "12.2" Print d1 ' prints 12.2 Print d2 ' prints 12.2

String Pattern Matching


This example explains how to do string pattern matching in Visual Basic. ' The Like operator pays attention to the current Option Compare setting. ' Setting it to text makes the pattern matching case-insensitive. Option Compare Text ' Use Option Compare Binary for case-sensitive string pattern matching. ' Option Compare Binary ' The Like string operator provides string pattern matching in Visual Basic. ' * matches zero or more of any character. ' ? matches any single character. ' # matches any single digit. ' Match ranges of characters by enclosing the range in brackets: ' [A-C] Matches any of A, B, or C ' [ABC] Same as [A-C] ' Match characters not in a range by using ! ' [!A-Z] Any character not including A-Z Dim s As String s = "Chilkat Software 1234 ABCD" If s Like "*Software*" Then Print "Contains Software" End If If s Like "[!0-9]??*" Then ' Match any three characters such that the first cannot be a digit. Print "Matches!" End If

String Replacement
This example shows how to do string replacement in Visual Basic. Dim s As String s = "Chilkat Software, Inc. : A software components company" ' Relace all occurances of a substring, case sensitive s = Replace(s, "software", "hardware") ' prints "Chilkat Software, Inc. : A Hardware Components Company" Print StrConv(s, vbProperCase) ' Replace all occurances, case-insensitive s = "Chilkat Software, Inc. : A software components company" s = Replace(s, "software", "hardware", 1, -1, vbTextCompare) ' prints "Chilkat Hardware, Inc. : A Hardware Components Company" Print StrConv(s, vbProperCase) ' Replace only the 1st occurance, case-insensitive startPos = 1 maxToReplace = 1 s = "Chilkat Software, Inc. : A software components company" s = Replace(s, "software", "hardware", startPos, maxToReplace, vbTextCompare) ' prints "Chilkat Hardware, Inc. : A Software Components Company" Print StrConv(s, vbProperCase)

Split String into Array of Words


This example shows how to do split a string into an array of words. Dim s As String s = "Chilkat Software, Inc. : A Software Components Company." Dim words() As String ' Split the string at the space characters. words() = Split(s) Dim tempStr As String For i = 0 To UBound(words) ' Eliminate punctuation tempStr = Replace(words(i), ".", "") tempStr = Replace(tempStr, ",", "") tempStr = Replace(tempStr, ":", "") If Len(tempStr) > 0 Then List1.AddItem tempStr End If Next

Split Comma-Separated String into Fields


This example shows how to do split a comma-separated string into fields. Dim s As String s = "Chilkat Mail, ActiveX Component, $99, free upgrades, 1-Year Support" Dim fields() As String ' Split the string at the comma characters and add each field to a ListBox fields() = Split(s, ",") For i = 0 To UBound(fields) List1.AddItem Trim$(fields(i)) Next

Determine if a String has been Initialized


This example shows how to do split a comma-separated string into fields. Dim s As String If StrPtr(s) = 0 Then Print "Not initialized!" Else Print "String is initialized." End If s = "123" If StrPtr(s) = 0 Then Print "Not initialized!" Else Print "String is initialized." End If

Das könnte Ihnen auch gefallen