middle ... (string function)

Prev Next

Function Names

middle, middle include, middle until, middle until include, vmiddle, vmiddle include, vmiddle until, vmiddle until include

Description

This function extracts a middle part of the string either with two character position numbers or matching patterns or a combination of both.

Vectorization: The function names beginning with the prefix v support vectorization in the 1st function parameter. Instead of providing a single value, you can provide a set or even a nested set which contain multiple values. The function will then process every value and its return value contains a corresponding set containing all results.
Note:For these functions, you need add the prefix letter 'v' in front of the function name. Otherwise the function recognizes a set and attempts to process sets and not strings. See middle [set function] for the corresponding set function.

  • middle:Returns middle part of string until specified character number (1 = 1st character) or first matching string
  • middle last match:Returns middle part of string until last first matching string
  • middle include: Like middle, but matching string is included in returned string
  • middle last match include: Like middle last match, but matching string is included in returned string
  • vmiddle:Like middle, vectorization applies if a set with strings is provided instead of a string.
  • vmiddle last match:Like middle last match, vectorization applies if a set with strings is provided instead of a string.
  • vmiddle include:Like middle include, vectorization applies if a set with strings is provided instead of a string.
  • vmiddle last match include:Like middle last match, vectorization applies if a set with strings is provided instead of a string.


Call as: function

Restrictions

Indirect parameter passing is disabled
Vectorization is allowed in the 1st function parameter

Parameter count

3

Parameters

No.TypeDescription
1
input
valid types input string or value(s)

Attention: Numerals and dates are converted to strings prior to processing. If 1st parameter is a set:

  • If the function name starts with the v prefix, the vectorization applies. Provide multiple values in a (nested) set to process all of them at once.
  • Otherwise, please refer to middle [set function] instead. Values of types other than string will be converted to strings first.

2
input
numeral starting character position

Positive: String begins with specified character position (0 = 1st character, 1 = 2nd character, etc.)
Larger than character count: Blank string is returned.
Negative: Starting position counted from right to left, e.g. -1 = begin with last character
Smaller than minus number of characters in the string: Begins with 1st character
The function name ending '... include' will be ignored here.

Alt. 2
input
string starting matching pattern

Function middle ...: Middle part of the string begins after the starting matching pattern.
Function middle include ...: Middle part of the string includes the starting matching pattern.
Empty matching pattern: Middle part of the string begins with the 1st character of the input string.

Matching patterns are case-sensitive.
Attention: The matching pattern parameter distinguishes between strings and softquoted strings. In softquoted strings, following wildcard symbols act as placeholders: '?' for any character, '#' for any digit and '^' for any letter.

3
input
numeral character count or ending character position

For function names middle and middle include, this parameter specifies the number of characters to include.
Zero: No characters to include. Empty string is returned.
Negative numbers are treated as zero.
Large character counts going beyond the end of the input string will be reduced accordingly.

For function names middle until and middle until include, this parameter specifies the ending position in the input string.
Character positions before the starting position of found pattern will return empty strings or at least the string matched with the matching pattern (applicable to middle until include).
Negative position numbers count from the last character position leftward.

Alt. 3
input
string ending matching pattern

Function middle ...: Middle part of the string ends before the ending matching pattern.
Function middle include ...: Middle part of the string includes the ending matching pattern.
Empty strings will be interpreted as no futher matches and returns empty strings.

Attention: The function names ending outside until combined with matching patterns provided will assert errors.
Attention: The matching pattern parameter distinguishes between strings and softquoted strings. In softquoted strings, following wildcard symbols act as placeholders: '?' for any character, '#' for any digit and '^' for any letter.

Return value

TypeDescription
string
set
Extracted substring(s)

In case vectorization has been applied, then the resulting strings are returned inside a (nested) set.

Examples

               st[] = "The taste of the coffee!";

               echo( middle( st[], 4, 8 ), " / ",  middle until ( st[], 4, 8 ) );
               echo( middle( st[], -6, 3 ), " / ", middle until( st[], -7, -2 ) ); // "off", "coffee"
               echo;
               echo( middle( st[],  99, 8 ), " / ",  middle until ( st[],  99, 8 ) ); // empty strings
               echo( middle( st[], -99, 8 ), " / ",  middle until ( st[], -99, 8 ) );
               echo;
               echo( middle( st[], 'taste ', 13 ), " / ", middle include( st[], 'taste ', 13 ) ); // "of the coffee", "tast of the coffee"
               echo;
               echo( middle until( st[], 'taste ', -2 ), " / ", middle until include( st[], 'taste ', -2 ) ); // "of the coffee", "tast of the coffee"
               echo( middle until include( st[], 'taste ', 3 ), " / ", middle until include( st[], 'taste ', 6 ) ); // "", "tas"
               echo;
               echo( middle ( st[], 'taste ', ' coffee' ), " / ", middle include( st[], 'taste ', ' coffee' ) ); // "of the", "tast of the coffee"

Output

taste of / taste
off / coffee

/
The tast / The taste

of the coffee / taste of the coffee

of the coffee / taste of the coffee
/ tas

of the / taste of the coffee
Try it yourself: Open LIB_Function_middle.b4p in B4P_Examples.zip. Decompress before use.

See also

left [string function]
right [string function]
outside [string function]
middle [set function]