# Mid, MidB
{: .no_toc }

Returns a **String** containing a specified number of characters from a string.

Syntax:

- **Mid$(** *string*, *start* [ **,** *length* ] **)**, **Mid(** *string*, *start* [ **,** *length* ] **)**
- **MidB$(** *string*, *start* [ **,** *length* ] **)**, **MidB(** *string*, *start* [ **,** *length* ] **)**

*string*
: *required* String expression from which characters are returned. If *string* contains **Null**, **Null** is returned.

*start*
: *required* **Long**. Character position in *string* at which the part to be taken begins. If *start* is greater than the number of characters in *string*, **Mid** returns a zero-length string (`""`).

*length*
: *optional* **Variant** (**Long**). Number of characters to return. If omitted or if there are fewer than *length* characters in the text (including the character at *start*), all characters from the *start* position to the end of the string are returned.

The `$`-suffixed forms return a **String**; the unsuffixed forms return a **Variant** (**String**).

To determine the number of characters in *string*, use the [**Len**](/tB/Modules/Strings/Len.md) function.

> [!NOTE]
> Use the **MidB** function with byte data contained in a string, as in double-byte character set languages. Instead of specifying the number of characters, the arguments specify numbers of bytes.

> [!TIP]
> Use the [**Mid =**](/tB/Core/Mid-equals.md) statement to replace characters within a string.

### Common Errors

| Error | When |
|---|---|
| [5 (`&H5`)](/Reference/Run-time-Errors.md#5), *Invalid procedure call or argument* | *start* is less than 1, or *length* is negative<br>**Mid** or **Mid$** is given a *start* greater than 1,073,741,824 or a *length* greater than 1,073,741,823 |
| [94 (`&H5E`)](/Reference/Run-time-Errors.md#94), *Invalid use of Null* | **Mid$** or **MidB$** is given a *string* that is **Null**, or any form is given a *start* or *length* that is **Null** |

### Example

This example uses the **Mid** function to return a specified number of characters from a string.

```tb check_build
Dim MyString, FirstWord, LastWord, MidWords
MyString = "Mid Function Demo"      ' Create text string.
FirstWord = Mid(MyString, 1, 3)     ' Returns "Mid".
LastWord = Mid(MyString, 14, 4)     ' Returns "Demo".
MidWords = Mid(MyString, 5)         ' Returns "Function Demo".
```

### See Also

- [Left](/tB/Modules/Strings/Left.md), [Len](/tB/Modules/Strings/Len.md), [Right](/tB/Modules/Strings/Right.md) functions
- [Mid =](/tB/Core/Mid-equals.md) statement
