# Text.LastIndexOf

> 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.

<!-- id: function-text-lastindexof · area: function · stability: stable · html: https://osysharp.com/reference/function/text-lastindexof/ -->

## Summary        {#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      {#signature}
```osy syntax
Text.LastIndexOf(<string> s, <string> sub) -> int
Text.LastIndexOf(<string> s, <string> sub, <int> startIndex) -> int
```

## Description    {#description}
Matching is **ordinal** (byte-for-byte, culture-independent), the same as its forward-search sibling
[Text.IndexOf](https://osysharp.com/reference/function/text-indexof/). A miss returns `-1`.

⚠ **For word-boundary truncation, reach for [Text.Truncate](https://osysharp.com/reference/function/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       {#examples}
```osy title="finding the index itself" test app=text-search
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       {#see-also}
- [Text.IndexOf](https://osysharp.com/reference/function/text-indexof/) — the forward-search sibling, with the same `startIndex` contract
