@Middle (JavaScript)

Returns characters of a string, starting at an offset from the beginning or after a substring, for the number of characters specified or up to and excluding a substring.

Defined in

@Functions (JavaScript)

Syntax

@Middle(value:string, offset:int, endString:string) : string

@Middle(value:string, offset:int, len:int) : string

@Middle(value:string, startString:string, endString:string) : string

@Middle(value:string, startString:string, len:int) : string

Parameter Description
value The string to check.
offset Offset from the first character. The function returns characters starting at this location. Offset 0 means the first character. A real number is truncated to an integer.
startString A substring. The function returns characters starting at the location following the first occurrence of the substring. An empty string means the location before the first character.
len Number of characters. The function returns the number of characters specified. A real number is truncated to an integer.
endString A substring. The function returns characters up to and excluding the first occurrence of the substring. An empty string means the location following the last character.
Return value Description
string The delineated characters.

Usage

This function returns:
  • An empty string if offset is outside the range of the string.
  • To the end of the string if len exceeds the remaining characters in the string.
  • An empty string if len is 0 or negative.
  • An empty string if startString is not found.
  • An empty string if endString is not found.

Examples

(1) This example gets characters from the first character up to the first space, the last character back to the last space, and character 7 through the character preceding the last space.
function p(stuff) {
	print("<<<" + stuff + ">>>");
}

var lineitem = "Q5212 peaches and cream 001.99";

var len = @Length(lineitem);
var code = @Middle(lineitem, 0, " ");
var price = @MiddleBack(lineitem, 0, " ");
var description = @Middle(lineitem, 6, " " + price);
p("Code = " + code); // Q5212
p("Price = " + price); // 001.99
p("Description = " + description); // peaches and cream

(2) This example gets characters from the first character for 5 characters, the last character back for 6 characters, and character 7 for the length of the string minus 13.

function p(stuff) {
	print("<<<" + stuff + ">>>");
}

var lineitem = "Q5212 peaches and cream 001.99";

var len = @Length(lineitem);
var code = @Middle(lineitem, 0, 5);
var price = @MiddleBack(lineitem, 0, -6);
var description = @Middle(lineitem, 6, len - 13);
p("Code = " + code); // Q5212
p("Price = " + price); // 001.99
p("Description = " + description); // peaches and cream

(3) This example gets characters from the first character up to the first space, the last character back to the last space, and the character following the first space up to last space.

function p(stuff) {
	print("<<<" + stuff + ">>>");
}

var lineitem = "Q5212 peaches and cream 001.99";

var len = @Length(lineitem);
var code = @Middle(lineitem, "", " ");
var price = @MiddleBack(lineitem, "", " ");
var description = @Middle(lineitem, " ", " " + price);
p("Code = " + code); // Q5212
p("Price = " + price); // 001.99
p("Description = " + description); // peaches and cream

(4) This example gets characters from the first character for 5 characters, the last character back for 6 characters, and the character following the first space for the length of the string minus 13.

function p(stuff) {
	print("<<<" + stuff + ">>>");
}

var lineitem = "Q5212 peaches and cream 001.99";

var len = @Length(lineitem);
var code = @Middle(lineitem, "", 5);
var price = @MiddleBack(lineitem, "", -6);
var description = @Middle(lineitem, " ", len - 13);
p("Code = " + code); // Q5212
p("Price = " + price); // 001.99
p("Description = " + description); // peaches and cream