172 of 323
ZBasic System Library 162 ZX Microcontroller Family Mid Type Function returning String Invocation Mid(str, pos, length) or Mid(str, pos) Parameter Method Type Description str ByVal* String The string from which to extract or modify a substring. pos ByVal int8/16 The position of the first character of the substring. length ByVal int8/16 The length of the substring to extract or modify. * When used on the left hand side of an assignment, this parameter is passed ByRef. Discussion This function can be used to extract a portion of a string or to modify a portion of a string, depending on how it is used. When it appears in the context of a function call, it returns a new string extracted from the string provided. The first character of the extracted substring will be the character at the position given by pos (where the first character of the string is position 1). The length of the returned string will be the number of characters in the source string beginning at the starting index through the end of the string or the specified length (if present), whichever is less. If the starting position is beyond the end of the string or if the specified length is less than or equal to zero, the returned string will be of zero length. When used on the left hand side of an assignment operator, the Mid() function replaces a sequence of characters in a string with characters from the string value on the right hand side of the assignment operator. Dim s as String s = "abcdef" Mid(str, 3) = "##" ' result is "ab##ef" Note that when used in this way the first parameter is passed by reference so it cannot be a literal string or any other entity than cannot be passed by reference. Also, the length of the target string will never be changed. The number of characters overwritten in the destination string will be the lesser of a) the number of characters in the string on the right hand side of the assignment, b) the number of characters specified in the third parameter (if present), and c) the number of characters in the target string beginning at the position specified by the second parameter through the end of the string. Compatibility In BasicX, the first parameter is pass-by-reference. This disallows any use of a string literal for the first parameter. Also, in BasicX the third parameter must always be provided. The BasicX documentation suggests that using Mid() on the left hand side of an assignment might result in a change in the string length. Tests indicate that this is not the case. Moreover, execution of the code fragment below actually results in a garbage character being placed in the third character position. Dim s as String s = "abc" Mid(s, 2, 2) = "!" ' result is "a!@" (@ is an indeterminate character) See Also Left, Right, Trim