< Day Day Up > |
The Strings module contains functions useful for manipulating string values. Public Function Asc(ByVal [String] As Char) As Integer Public Function Asc(ByVal [String] As String) As Integer The Asc function returns the ASCII value of the character or the first character in the string.
Public Function AscW(ByVal [String] As String) As Integer Public Function AscW(ByVal [String] As Char) As Integer The AscW function returns the Unicode value of the character or the first character in the string. Public Function Chr(ByVal CharCode As Integer) As Char The Chr function returns a character representing the given ASCII character value.
Public Function ChrW(ByVal CharCode As Integer) As Char The ChrW function returns a character representing the given Unicode character value. Public Function Filter(ByVal Source() As Object, _ ByVal Match As String, Optional ByVal Include As Boolean = True, _ Optional ByVal Compare As CompareMethod = CompareMethod.Binary) _ As String() Public Function Filter(ByVal Source() As String, _ ByVal Match As String, Optional ByVal Include As Boolean = True, _ Optional ByVal Compare As CompareMethod = CompareMethod.Binary) _ As String() The Filter function returns a one-dimensional array of strings representing the members of the Source array filtered on the Match parameter. If Include is True , the array contains all elements of Source that contain Match as a substring. If Include is False , the array contains all elements of Source that do not contain Match as a substring. Public Function Format(ByVal Expression As Object, _ Optional ByVal Style As String = "") As String The Format function formats a value based on the specified format string. See Format Strings at the end of this section for more information on format strings. Public Function FormatCurrency(ByVal Expression As Object, _ Optional ByVal NumDigitsAfterDecimal As Integer = -1, _ Optional ByVal IncludeLeadingDigit As TriState = _ TriState.UseDefault, _ Optional ByVal UseParensForNegativeNumbers As TriState = _ TriState.UseDefault, _ Optional ByVal GroupDigits As TriState = TriState.UseDefault) _ As String The FormatCurrency function formats a value into a currency string. The currency format used is the current system currency format at runtime. Public Function FormatDate(ByVal Expression As Date, _ Optional ByVal NamedFormat As DateFormat = DateFormat.GeneralDate) _ As String The FormatDate function formats a value based on the specified date format. Public Function FormatNumber(ByVal Expression As Object, _ Optional ByVal NumDigitsAfterDecimal As Integer = -1, _ Optional ByVal IncludeLeadingDigit As TriState = _ TriState.UseDefault, _ Optional ByVal UseParensForNegativeNumbers As TriState = _ TriState.UseDefault, _ Optional ByVal GroupDigits As TriState = TriState.UseDefault) _ As String The FormatNumber function formats a value into a number string. The number format used is the current system number format at runtime. Public Function FormatPercent(ByVal Expression As Object, _ Optional ByVal NumDigitsAfterDecimal As Integer = -1, _ Optional ByVal IncludeLeadingDigit As TriState = _ TriState.UseDefault, _ Optional ByVal UseParensForNegativeNumbers As TriState = _ TriState.UseDefault, _ Optional ByVal GroupDigits As TriState = TriState.UseDefault) _ As String The FormatPercent function formats a value into a percentage string. The number format used is the current system number format at runtime. Public Function GetChar(ByVal str As String, ByVal Index As Integer) _ As Char The GetChar function returns the character position specified by Index . Public Function InStr(ByVal String1 As String, _ ByVal String2 As String, _ Optional ByVal Compare As CompareMethod = CompareMethod.Binary) _ As Integer Public Function InStr(ByVal Start As Integer, _ ByVal String1 As String, ByVal String2 As String, _ Optional ByVal Compare As CompareMethod = CompareMethod.Binary) _ As Integer The InStr function returns an Integer specifying the start position of the first occurrence of one string within another. Public Function InStrRev(ByVal StringCheck As String, _ ByVal StringMatch As String, Optional ByVal Start As Integer = -1, _ Optional ByVal Compare As CompareMethod = CompareMethod.Binary) _ As Integer The InStrRev function returns an Integer specifying the start position of the last occurrence of one string within another. Public Function Join(ByVal SourceArray() As Object, _ Optional ByVal Delimiter As String = " ") As String Public Function Join(ByVal SourceArray() As String, _ Optional ByVal Delimiter As String = " ") As String The Join function returns a string that represents the concatenation of all the values passed in. If Delimiter is supplied, the string is used to separate each value in the string. Public Function LCase(ByVal Value As String) As String Public Function LCase(ByVal Value As Char) As Char The LCase function converts a string into all lowercase. Public Function Left(ByVal str As String, ByVal Length As Integer) _ As String The Left function returns the specified number of characters in the string, starting from the beginning of the string. Public Function Len(ByVal Expression As String) As Integer Public Function Len(ByVal Expression As Byte) As Integer Public Function Len(ByVal Expression As Char) As Integer Public Function Len(ByVal Expression As Int16) As Integer Public Function Len(ByVal Expression As Int32) As Integer Public Function Len(ByVal Expression As Int64) As Integer Public Function Len(ByVal Expression As Single) As Integer Public Function Len(ByVal Expression As Double) As Integer Public Function Len(ByVal Expression As Boolean) As Integer Public Function Len(ByVal Expression As Decimal) As Integer Public Function Len(ByVal Expression As Date) As Integer Public Function Len(ByVal Expression As Object) As Integer The Len function returns the number of characters required to store the value in a file using the functions in the FileSystem module. Public Function LSet(ByVal Source As String, ByVal Length As Integer) _ As String The LSet function returns a string of the specified length. If the Source parameter is less than Length characters long, the returned string is padded at the end with spaces. Public Function LTrim(ByVal str As String) As String The LTrim function returns the str parameter with all leading spaces removed. Public Function Mid(ByVal str As String, ByVal Start As Integer) _ As String Public Function Mid(ByVal str As String, ByVal Start As Integer, _ ByVal Length As Integer) As String The Mid function returns the specified number of characters from the middle of a string. If no Length parameter is specified, the rest of the string is returned. It is also possible to use the Mid function to assign a string into another string. The following example prints the string abxxef . Dim s As String = "abcdef" Mid(s, 3, 2) = "xx" Console.WriteLine(s) When Mid assignment is done, the string value on the right-hand side of the assignment replaces the characters in the right-hand side string indicated by the Start and Length parameters of the function. Only the number of characters in the right-hand side string is replaced , and no more than the number of characters selected by the Mid function are replaced.
Public Function Replace(ByVal Expression As String, _ ByVal Find As String, ByVal Replacement As String, _ Optional ByVal Start As Integer = 1, _ Optional ByVal Count As Integer = -1, _ Optional ByVal Compare As CompareMethod = CompareMethod.Binary) _ As String The Replace function returns a string in which a specified substring has been replaced with another substring a specified number of times. Public Function Right(ByVal str As String, ByVal Length As Integer) _ As String The Right function returns the specified number of characters in the string, starting from the end of the string. Public Function RSet(ByVal Source As String, ByVal Length As Integer) _ As String The RSet function returns a string of the specified length. If the Source parameter is less than Length characters long, the returned string is padded at the beginning with spaces. Public Function RTrim(ByVal str As String) As String The RTrim function returns the str parameter with all trailing spaces removed. Public Function Space(ByVal Number As Integer) As String The Space function returns a string consisting of the specified number of spaces. Public Function Split(ByVal Expression As String, _ Optional ByVal Delimiter As String = " ", _ Optional ByVal Limit As Integer = -1, _ Optional ByVal Compare As CompareMethod = CompareMethod.Binary) _ As String() The Split function returns an array of substrings that represent the partitioning of the original string. The original string is partitioned based on the Delimiter parameter. The Limit parameter determines how many substrings will be returned. Public Function StrComp(ByVal String1 As String, _ ByVal String2 As String, _ Optional ByVal Compare As CompareMethod = CompareMethod.Binary) _ As Integer The StrComp function compares two strings using the alphanumeric sort values of the corresponding character of each string. If String1 sorts before String2 , the function returns “1. If String1 sorts after String2 , the function returns 1. If the two strings sort the same, the function returns 0. Public Function StrConv(ByVal str As String, _ ByVal Conversion As VbStrConv, _ Optional ByVal LocaleID As Integer = 0) As String The StrConv function converts the specified string into a new string using the specified conversion and locale. Public Function StrDup(ByVal Number As Integer, _ ByVal Character As Object) As Object Public Function StrDup(ByVal Number As Integer, _ ByVal Character As Char) As String Public Function StrDup(ByVal Number As Integer, _ ByVal Character As String) As String The StrDup function returns a string that consists of the given character repeated a specified number of times. Public Function StrReverse(ByVal Expression As String) As String The StrReverse function returns a string that is the reverse of the specified string. Public Function Trim(ByVal str As String) As String The Trim function returns the str parameter with all leading and trailing spaces removed. Public Function UCase(ByVal Value As String) As String Public Function UCase(ByVal Value As Char) As Char The UCase function converts a string into all uppercase. Format StringsA format string controls how the Format function formats a value into a string. By default, an empty format string will convert the value to a string using the current regional settings of the computer at runtime. Format also has a set of predefined formats that can be used to format values in a standard way. And, finally, user -defined formats can be composed using special characters. Predefined FormatsThe predefined formats are named formats. To use a predefined format, you supply the name of the format as the Style parameter of the Format function. Tables A-2 through A-4 list the predefined formats. Table A-2. Numeric Predefined Formats
Table A-3. Date/Time Predefined Formats
Table A-4. Other Predefined Formats
User-Defined FormatsA user-defined format string uses special characters to indicate how the value should be formatted. Tables A-5 and A-6 list the characters that can be used to create format strings. These are some examples of user-defined numeric format strings. Format(10, "") ' Result:10 Format(41, "000") ' Result: 041 Format(3.5, "0.00") ' Result: 3.50 Format(35, "#,##0") ' Result: 35 Format(1000, "#,##0") ' Result: 1,000 Format(1394.34, "$#,##0.00") ' Result: 94.34 Format(.124, "0.00%") ' Result: 12.40% Format(.124, "0.00E-00") ' Result: 1.24E-01 Table A-5. Numeric User-Defined Format Characters
Table A-6. Date/Time User-Defined Format Characters
These are some examples of user-defined date format strings. Format(#8/23/70 3:43:12AM#, "M/d/yy") ' Result: 8/23/70 Format(#8/23/70 3:43:12AM#, "d-MMM") ' Result: 23-Aug Format(#8/23/70 3:43:12AM#, "d-MMMM-yy") ' Result: 23-August-1970 Format(#8/23/70 3:43:12AM#, "d MMMM") ' Result: 23 August Format(#8/23/70 3:43:12AM#, "MMMM yy") ' Result: August 70 Format(#8/23/70 3:43:12AM#, "hh:mm tt") ' Result: 03:43 AM Format(#8/23/70 3:43:12AM#, "h:mm:ss") ' Result: 3:43:12 Within a format string, the backslash character ( \ ) causes the next character to be put into the output instead of being interpreted as a format character. The backslash itself is not displayed. (To display a backslash, you have to use two backslashes.) For example: ' Result is: Phone # is 206-555-1212 Format(2065551212, "Phone \# is: ###-###-####") ' Result is: C:\Test10.txt Format(10, "C\:\Test10\.txt") Strings in quotation marks are also copied directly into the output. The quotes themselves are not displayed. (To display a double quote character, you must precede it by a backslash.) For example: ' Result is: Phone # is 206-555-1212 Format(2065551212, "Phone ""#"" is: ###-###-####") ' Result is: The value is "10". Format(10, "The value is \""##\""\.") Numeric formats can format differently based on the value of the number being formatted. If the format string has two formats separated by a semicolon, the first format applies to values equal to or greater than zero, while the second format applies to values less than zero. If the format string has three formats separated by semicolons, the first format applies to positive values, the second format applies to negative values, and the third format applies to zero values. If a format string with three formats omits the second format, the first format is used for both positive and negative numbers . For example: Format(1234, "$#,##0;($#,##0)") ' Result: ,234 Format(-1234, "$#,##0;($#,##0)") ' Result: (,234) Format(1234, "$#,##0;($#,##0);\Z\e\r\o") ' Result: ,234 Format(0, "$#,##0;($#,##0);\Z\e\r\o") ' Result: Zero Format(1234, "$#,##0;;\Z\e\r\o") ' Result: ,234 Format(0, "$#,##0;;\Z\e\r\o") ' Result: Zero |
< Day Day Up > |