@MiddleBack (JavaScript)

Returns characters of a string, starting at an offset from the end or before the last occurrence of a substring, back to and excluding the last occurrence of a substring or for the negative number of characters specified.

Defined in

@Functions (JavaScript)

Syntax

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

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

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

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

Parameter Description
value The string to check.
offset Offset from the last character. The function returns characters starting at this location. Offset 0 means the last character. A real number is truncated to an integer.
startString A substring. The function returns characters starting at the location before the last occurrence of the substring. An empty string means the location after the last character.
len Negative number of characters. The function returns the number of characters specified. A real number is truncated to an integer. The number must be negative.
endString A substring. The function returns characters back to and excluding the last occurrence of the substring. An empty string means the location after 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 positive.
  • 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