Skip to main content
Previous sectionNext section

Mid

Returns or replaces a specified number of characters from a string.

Synopsis

Mid(string,start[,length])

Mid(string,start[,length])=value

Arguments

string String expression from which characters are returned.
start A positive integer specifying the character position in string (counting from 1) at which the substring begins.
length Optional — A positive integer specifying the number of characters to return (or replace) from the start location (inclusive). If length is omitted, all characters from the start position to the end of the string are returned (or replaced).
value The value used to replace the specified character(s) in string. An expression that evaluates to a string.

Description

The Mid function can be used in two ways:

  • To return a substring from string. The substring is determined by specifying the start position and, optionally, the length. This uses the Mid(string,start,length) syntax.

  • To replace a substring within string. The replacement substring may be the same length, longer, or shorter than the original substring. The substring is determined by specifying the start position and, optionally, the length. This uses the Mid(string,start,length)=value syntax.

Mid(string,start,length) returns the character(s) of string specified by the start and length arguments. The length argument specifies how many characters of string to return. If length is 0 or a negative number, Mid returns the empty string (""). If you specify a length greater than the number of available characters, all of the characters to the right of start are returned.

Mid(string,start,length)=value replaces the specified character(s) of string with value.

The start argument specifies where to begin the replacement. If start is 1, begin at the beginning of string. If start is greater than the length of string, the string is padded with blank spaces until start is reached, then value is appended. If start is 0 or a negative number, the number of characters replaced is start plus length minus 1.

The optional length argument specifies how many characters of string to replace. If length is omitted, all of the characters to the right of start are replaced. If length is 0 or a negative number, string is unchanged. This is true even when string is the empty string ("").

To determine the number of characters in string, use the Len function.

Note that the length argument refers to the source length, not the replacement length, which may be longer or shorter than the substring replaced.

You can perform similar substring return and replace operations in ObjectScript using the $EXTRACT function.

Examples

The following example returns a substring without specifying a length. It begins with the twelfth character (inclusive) and returns the rest of the string:

Dim MyVar
MyVar = Mid("Caché is a powerful database!",12)
Println MyVar   ' Returns "powerful database!"
Copy code to clipboard

The following example returns a substring specifying a length. It begins with the twelfth character in a string, and returns eight characters:

Dim MyVar
MyVar = Mid("Caché is a powerful database!",12,8)
Println MyVar     ' Returns "powerful"
Copy code to clipboard

In the following example, all of the Mid functions return the empty string:

Dim MyVar
MyVar = Mid("Caché is a powerful database!",0,8)
Println "0,n=",MyVar
MyVar = Mid("Caché is a powerful database!",8,0)
Println "n,0=",MyVar
MyVar = Mid("Caché is a powerful database!",8,-1)
Println "n,-1=",MyVar
Copy code to clipboard

The following example show the difference between specifying a numeric as the string argument, and specifying the same value as a string. Numerics are converted to canonical form, which in this case means that leading zeros are omitted.

Println Mid(00123456,3,2)    ' Returns 34
Println Mid("00123456",3,2)  ' Returns 12
Copy code to clipboard

Replacement Examples

The following example replaces characters starting with specified start location to the end of the string:

var1="ABCD"
var2="ABCD"
var3="ABCD"
var4="ABCD"
var5="ABCD"
PrintLn var
Mid(var1,2)="Z"
PrintLn "start 2: ",var1
Mid(var2,8)="Z"
PrintLn "start 8: ",var2
Mid(var3,1)="Z"
PrintLn "start 1: ",var3
Mid(var4,0)="Z"
PrintLn "start 0: ",var4
Mid(var5,-1)="Z"
PrintLn "start -1: ",var5
Copy code to clipboard

The following example starts at various specified start locations and replaces length=2 characters:

var1="ABCD"
var2="ABCD"
var3="ABCD"
Mid(var1,2,2)="xyz"
PrintLn "start 2: ",var1
Mid(var2,8,2)="xyz"
PrintLn "start 8: ",var2
Mid(var3,1,2)="xyz"
PrintLn "start 1: ",var3
Copy code to clipboard

The following example starts at start=2 and replaces various length values:

var1="ABCD"
var2="ABCD"
var3="ABCD"
var4="ABCD"
var5="ABCD"
Mid(var1,2,2)="xyz"
PrintLn "length 2: ",var1
Mid(var2,2,8)="xyz"
PrintLn "length 8: ",var2
Mid(var3,2,1)="xyz"
PrintLn "length 1: ",var3
Mid(var4,2,0)="xyz"
PrintLn "length 0: ",var4
Mid(var5,2,-1)="xyz"
PrintLn "length -1: ",var5
Copy code to clipboard

The following example demonstrates replacement with start locations less than 1. The formula is start+length-1:

var1="ABCD"
var2="ABCD"
var3="ABCD"
Mid(var1,-2,5)="xyz"
PrintLn "start -2 for 5: ",var1
Mid(var2,0,1)="xyz"
PrintLn "start 0 for 1: ",var2
Mid(var3,0,2)="xyz"
PrintLn "start 0 for 2: ",var3
Copy code to clipboard

See Also