Midb
Syntax:¶
MIDB(text, start_num, num_bytes)
- text: The text string containing the characters you want to extract. This can be a direct string, a cell reference, or the result of another function.
- start_num: The position of the first character you want to extract, counted in bytes. The first byte in
textis 1. - num_bytes: The number of bytes to extract from the
text, starting atstart_num.
Description:¶
The MIDB function in Excel returns a specific number of characters from a text string, starting at the position you
specify, based on the number of bytes rather than the number of characters. This distinction is important when working
with double-byte character set (DBCS) languages such as Japanese, Chinese, and Korean, where a single character can
occupy two bytes. In single-byte character set (SBCS) languages such as English, MIDB behaves identically to the
MID function because each character is one byte.
Its flexible parameters allow selection of any subset of the string based on byte positions, which is particularly useful when processing text that contains a mix of single-byte and double-byte characters.
For example:
MIDB("text", start_num, num_bytes) = extracted substring (by bytes)
Examples:¶
=MIDB("HelloWorld", 1, 5)- Returns:
"Hello"(In an SBCS environment, the first 5 bytes correspond to the first 5 characters.)
- Returns:
=MIDB("HelloWorld", 6, 5)- Returns:
"World"(Starts at the 6th byte and extracts 5 bytes.)
- Returns:
=MIDB("Excel Functions", 7, 9)- Returns:
"Functions"(In an SBCS environment, this is equivalent to=MID("Excel Functions", 7, 9).)
- Returns:
=MIDB(A1, 3, 4)- Returns 4 bytes of text from cell A1 starting at the 3rd byte.
(In an SBCS environment, this is equivalent to
=MID(A1, 3, 4).)
- Returns 4 bytes of text from cell A1 starting at the 3rd byte.
(In an SBCS environment, this is equivalent to
=MIDB("Excel", 2, 10)- Returns:
"xcel"(Ifnum_bytesexceeds the remaining byte length of the text fromstart_num, all characters fromstart_numto the end are returned.)
- Returns:
Notes:¶
- In SBCS languages (e.g., English),
MIDBreturns the same results asMIDbecause each character equals one byte. The difference only becomes apparent in DBCS languages where characters may occupy two bytes. - If
start_numis greater than the byte length oftext, theMIDBfunction will return an empty string (""). - If
start_numis less than 1, Excel will return a#VALUE!error. - If
num_bytesis negative, Excel will return a#VALUE!error. - If
num_bytesis 0, an empty string ("") is returned. - If
start_num + num_bytes - 1exceeds the byte length oftext, the function will return all bytes fromstart_numto the end of thetext. - The
start_numandnum_bytesarguments count bytes, not characters. When working with DBCS text, be mindful that a double-byte character occupies positions for two bytes. - For character-based extraction (rather than byte-based), use the
MIDfunction. - The
MIDBfunction is equivalent toMIDin Excel when the default language setting is a single-byte character set language. In DBCS language environments,MIDBcounts each double-byte character as 2 bytes.