Difference between revisions of "Mid"

From Xojo Documentation

Line 9: Line 9:
<font  color="green"><i> Introduced 5.0</i></font><br/>
''result''='''Mid'''(''source'', ''start'' [,''length''])<br />
''result''='''Mid'''(''source'', ''start'' [,''length''])<br />
'''OR'''<br />
'''OR'''<br />

Latest revision as of 16:51, 7 April 2021

Mid (the global function) is here for compatibility with Visual Basic. You should instead use String.Middle, although note that Mid is 1-based and Middle is 0-based.

Returns a portion of a string. The first character is numbered 1.


result=Mid(source, start [,length])
result=stringVariable.Mid(start [,length])

Part Type Description
result String The portion of source from start and continuing for length characters or all remaining characters if length is not specified.
source String Required. The string from which characters are returned.
start Integer Required. The position of the first character to be returned.

The first character position is 1. If start is greater than the number of characters in source, an empty string is returned.

length Integer Optional. The number of characters to return from source.

If omitted, all characters from start to the end of source are returned. If length + start is greater than the length of source, all characters from start to the end of source will be returned.

stringVariable String Any variable of type String.


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

The Mid function works properly with international text.


These examples use the Mid function to return portions of a string.

Var s As String
s = Mid("This is a test", 6) // returns "is a test"
s = Mid("This is a test", 11, 4) // returns "test"

s = "This is a test"
s = s.Mid(11, 4) // returns "test"

This example converts the text string in TextField1 to hex and writes the result to TextField2:

TextField2.Value = ""
For i As Integer = 1 To Len(TextField1.Value)
TextField2.Value = TextField2.Value + "&h" + Hex(Asc(Mid(TextField1.Value, i, 1)))

See Also

Asc, Chr, InStr, Left, Len, Right functions.