VBScript mid Function

Source: Internet
Author: User
Tags rtrim
[Name]
Mid
Category]
String Functions
[Prototype]
Mid (string, start [, length])
[Parameters]
The syntax of the MID function has the following naming parameters:
Part
Description
String
Required parameter. String expression, which returns characters. If string contains null, null is returned.
Start
Required parameter. Long. String. If start exceeds the string Character Count, Mid Returns a zero-length string ("").
Length
Optional; variant (long ). The number of characters to return. If the value is omitted or the length exceeds the number of characters in the text (including the characters at the start), all characters from start to the end of the string are returned.

[Return value]
Returns the variant (string) data type.
[Exception/Error]
None
[Description]
Returns the string starting from the start position specified by start. The length is truncated. If the value is omitted or the length exceeds the number of characters in the text (including the characters at the start), all characters from start to the end of the string are returned.
[Example]
In this example, the mid statement is used to obtain several characters in a string.
Dim mystring, firstword, lastword, midwords
Mystring = "mid function Demo" creates a string.
Firstword = mid (mystring, 1, 3) 'returns "mid ".
Lastword = mid (mystring, 14, 4) 'returns "Demo ".
Midwords = mid (mystring, 5) 'returns "funcion Demo ".
[Remarks]
The number of characters in string. The LEN function is available.
Note: The midb function acts on the byte data contained in a string, just as in the dual-byte character set (DBCS) language. Therefore, the parameter specifies the number of bytes rather than the number of characters. For sample code using midb, see the second example in the example topic.
The following table lists the function value types and functions.
String Functions
Function Name
Function value type
Function
Example
CHR $ (N)
String
Evaluate the characters with N as ASCII code
CHR (65) = ""
STR $ (N)
String
Converts n to a string. If n> 0, the returned string contains a leading space, indicating that there is a positive number.
STR $ (-12345) = "-12345"
STR (12345) = "12345"
Len (c)
Long
Evaluate the number of characters contained in string C
Len ("Abab string 4") = 8
Ucase $ (c)
String
Converts lowercase English letters in string C to uppercase letters.
Ucase ("abababc") = "abababc"
Lcase $ (c)
String
Converts uppercase English letters in string C to lowercase English letters.
Ucase ("abababc") = "abababc"
Space $ (N)
String
Generate a string consisting of n Spaces
Len (space (6) = 6
String $ (N, C)
String
Generates n strings consisting of the first character specified by C. C can be the number of ASCII codes.
String (6, "ABC") = "aaaaaa"
String (6, 65) = "aaaaaa"
Left (C, N)
String
Truncates n characters from the leftmost part of string C.
Left $ ("abcdef", 3) = "ABC"
Right $ (C, N)
String
Truncates n characters from the rightmost of string C.
Right $ ("abcdef", 3) = "def"
Mid $ (C, N1 [, N2])
String
Truncates N2 characters from the starting position specified by N1 in string C.
Mid ("abcdef", 2, 3) = "BCD"
Mid ("abcdef", 2) = "bcdef"
Ltrim $ (c)
String
Deletes leading spaces in string C.
Ltrim ("ABCD") = "ABCD"
Rtrim $ (c)
String
Delete trailing spaces in string C
Ltrim ("ABCD") = "ABCD"
Trim $ (c)
String
Removes leading and trailing spaces from string parameters.
Ltrim ("ABCD") = "ABCD"
Strcomp (C1, C2 [, N])
Integer
Compares the sizes of string C1 and string C2. N is a comparison type. Values: 0, 1, or 2.
Strcomp ("ABC", "ABC", 1) = 0
Strcomp ("ABC", "ABC", 0) =-1
Strcomp ("ABC", "ABC", 0) = 1
Instr ([N1,] C1, C2 [, N2])
Integer
In the string C1, start from N1 to N2 and start to find C2. If N1 is omitted, start from C1 header and end from C1 When N2 is omitted. The function value is 0 when it cannot be found.
Instr (2, "ABCDE", "C", 4) = 3
Instr (2, "abcdef", "CDE") = 3
Instr ("abcdefgh", "CDE") = 3
Instr ("abcdefgh", "xy") = 0
(1) The C expression in the table is a string expression, and the N expression in the table is a numerical expression.
(2) Val (C), comma (,), and dollar sign ($) cannot be recognized. spaces, tabs, and linefeeds are removed from the parameters; when the letter E or D is encountered, it is processed by a single-precision or double-precision real-type floating point number.
(3) lenb (c) functions are similar to Len (c) functions, except that lenb functions calculate the number of bytes rather than the number of characters in the string. For example, lenb ("abcabc123") = 18, lenb ("string") = 6.
(4) For the string function string $ (N, C), where the C parameter can be any valid numeric expression or string expression. If it is a numeric expression, the ASCII code of the character that constitutes the string. If it is a string expression, the first character is used to generate the string.
(5) For the left cut function left (C, N) of the string, the N parameter is a numeric expression, and its value indicates the number of characters in the function value. If its value is 0, the function value is a zero-length string (an empty string). If the value is greater than or equal to the number of characters in string C, the function value is the entire string.
The leftb function is similar to the left function, except that the leftb function calculates the number of bytes rather than the number of characters in the string. Leftb ("ABCDE", 6) = "ABC ".
(6) For the right truncation function right (C, N) of a string, the N parameter is a numeric expression, and its value indicates the number of characters in the function value. If the value is 0, the function value is an empty string. If the value is greater than or equal to the number of characters in string C, the function value is the entire string.
The rightb function is similar to the right function, except that the rightb function calculates the number of bytes rather than the number of characters in the string. Rightb ("ABCDE", 6) = "CDE ".
(7) N1 is a numeric expression whose value indicates the starting position of the Start character. If the value exceeds the number of characters in string C, the function value is an empty string. N2 is a numeric expression whose value indicates the number of characters to be truncated. If this parameter is omitted, the function value contains all the characters from the starting position to the end of the string in string C.
The midb function is similar to the mid function, except that the midb function calculates the number of bytes of the string, rather than the number of characters in the string. For example, midb ("abcdefg", 5, 8) = "cdef ".
(8) When a string is assigned a certain value to a long string variable, if the length of the string variable is greater than the length of the string, use spaces to fill the excess part at the end of the string variable, therefore, the ltrim and rtrim functions that delete spaces are very useful when processing fixed-length string variables.
(9) For the string comparison function strcomp (C1, C2 [, N]), n is the comparison type of the specified string. The comparison type can be 0, 1, or 2. If the comparison type is 0, binary comparison is executed. English letters are case-sensitive. If the comparison type is 1, text comparison is executed, at this time, English letters are not case sensitive. If the comparison type is 2, the comparison is performed based on the database information and only applies to Microsoft Access. If this parameter is omitted, the default comparison type is 0.
When string C1 is less than string C2, the function value is-1. When string C1 is equal to string C2, the function value is 0: When string C1 is greater than string C2, the function value is 1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.