Osy#the first language built for agents
Agents firstAgentic appsWorkflowsDurable Execution — built inSecurityTestingThe editorThe UI modelOne program

Reference / Function

Text.LastIndexOf

Text.LastIndexOf(s, sub[, startIndex]) → int (last ordinal match, -1 on miss)

The C# string.LastIndexOf: the index of the LAST ordinal occurrence of a substring, or -1 if absent. The 3-arg overload starts the backward search at startIndex (searching toward the beginning). Runs in memory only — there is no SQL push-down form.

stable1 example compiled by CIfunctiontextstdlib

Summary#

Text.LastIndexOf(s, sub) returns the zero-based index of the last ordinal occurrence of sub in s, or -1 when sub does not occur — exactly C#'s string.LastIndexOf. The 3-arg overload Text.LastIndexOf(s, sub, startIndex) begins the search at startIndex and proceeds backward toward the beginning, matching C#'s LastIndexOf(value, startIndex).

Signature#

Text.LastIndexOf(<string> s, <string> sub) -> int
Text.LastIndexOf(<string> s, <string> sub, <int> startIndex) -> int

Description#

Matching is ordinal (byte-for-byte, culture-independent), the same as its forward-search sibling Text.IndexOf. A miss returns -1.

For word-boundary truncation, reach for Text.Truncate instead. It is the same idea done once and correctly — it bounds the result by the budget including the ellipsis, handles a single long word and a leading space, and trims the trailing space. Hand-rolled versions of it (including the Clip below) routinely overshoot the budget they were given. Use Text.LastIndexOf directly when you want the INDEX for something else.

Text.LastIndexOf is in-memory only — like Text.Reverse, it has no faithful SQL form, so it may be called on locals inside a function body but not pushed down into a query predicate.

The 3-arg startIndex is the C# contract: it is the last position considered, and the search runs backward. As in C#, an out-of-range startIndex faults rather than clamping.

Examples#

string Clip(string content, int budget) {
  var cut = Text.LastIndexOf(content, " ", budget);   // last space at/before the budget
  if (cut < 0) {
    return content;                                    // no space → keep whole
  }
  return Text.Substring(content, 0, cut);
}
// Clip("the quick brown fox", 12)  ->  "the quick"

See also#

  • Text.IndexOf — the forward-search sibling, with the same startIndex contract

Related

Text.Truncate

Shorten a string to at most maxLength characters — INCLUDING the ellipsis — cutting back to the last word boundary…

String interpolation & format specifiers

Build a string from literal text and embedded expressions with $"…{expr}…". A hole may carry a .NET format specifier…