---
title: "String Operations on Words"
canonical: "https://docs.magma-maths.org/v2.30-1/FinitelyPresentedGroups/FinitelyPresentedSemigroups/operation-element.html"
source: "https://docs.magma-maths.org/v2.30-1/FinitelyPresentedGroups/FinitelyPresentedSemigroups/operation-element.md"
part: "Finitely Presented Groups"
chapter: "Finitely Presented Semigroups"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "Finitely Presented Groups", "source": "https://docs.magma-maths.org/v2.30-1/FinitelyPresentedGroups/index-finitely-presented-groups.md"}
  - {"title": "Finitely Presented Semigroups", "source": "https://docs.magma-maths.org/v2.30-1/FinitelyPresentedGroups/FinitelyPresentedSemigroups/index-finitely-presented-semigroups.md"}
prev: {"title": "Elementary Tietze Transformations", "source": "https://docs.magma-maths.org/v2.30-1/FinitelyPresentedGroups/FinitelyPresentedSemigroups/modification-Tietze.md"}
next: {"title": "Monoids Given by Rewrite Systems", "source": "https://docs.magma-maths.org/v2.30-1/FinitelyPresentedGroups/MonoidsGivenByRewriteSystems/index-monoids-given-by-rewrite-systems.md"}
intrinsics:
  - {"name": "Eliminate", "signature": "Eliminate(u, x, v): SgpFPElt, SgpFPElt, SgpFPElt -> SgpFPElt"}
  - {"name": "Match", "signature": "Match(u, v, f): SgpFPElt, SgpFPElt, RngIntElt -> BoolElt, RngIntElt"}
  - {"name": "Random", "signature": "Random(S, m, n): SgpFP, RngIntElt, RngIntElt -> SgpFPElt"}
  - {"name": "RotateWord", "signature": "RotateWord(u, n): SgpFPElt, RngIntElt -> SgpFPElt"}
  - {"name": "Substitute", "signature": "Substitute(u, f, n, v): SgpFPElt, RngIntElt, SgpFPElt, RngIntElt -> SgpFPElt"}
  - {"name": "Subword", "signature": "Subword(u, f, n): SgpFPElt, RngIntElt, RngIntElt -> SgpFPElt"}
  - {"name": "ElementToSequence", "signature": "ElementToSequence(u): SgpFPElt -> [ SgpFPElt ]"}
  - {"name": "Eltseq", "signature": "Eltseq(u): SgpFPElt -> [ SgpFPElt ]"}
---

# String Operations on Words

<a id="function-eliminate-sgpfpelt-sgpfpelt-sgpfpelt"></a>
## `Eliminate(u, x, v): SgpFPElt, SgpFPElt, SgpFPElt -> SgpFPElt`

Given words $u$ and $v$, and a generator $x$, belonging to a semigroup $S$, return the word obtained from $u$ by replacing each occurrence of $x$ by $v$.

<a id="function-match-sgpfpelt-sgpfpelt-rngintelt"></a>
## `Match(u, v, f): SgpFPElt, SgpFPElt, RngIntElt -> BoolElt, RngIntElt`

Suppose $u$ and $v$ are words belonging to the same semigroup $S$, and that $f$ is an integer such that $1 \leq f \leq \# u$. If $v$ is a subword of $u$, the function returns true, as well as the least integer $l$ such that:

**(a)**
$l\geq f$; and,

**(b)**
$v$ appears as a subword of $u$, starting at the $l$-th letter of $u$.

If no such $l$ is found, `Match` returns only false.

<a id="function-random-sgpfp-rngintelt-rngintelt"></a>
## `Random(S, m, n): SgpFP, RngIntElt, RngIntElt -> SgpFPElt`

A random word of length $l$ in the generators of the semigroup $S$, where $m \leq l \leq n$.

<a id="function-rotateword-sgpfpelt-rngintelt"></a>
## `RotateWord(u, n): SgpFPElt, RngIntElt -> SgpFPElt`

The word obtained by cyclically permuting the word $u$ by $n$ places. If $n$ is positive, the rotation is from left to right, while if $n$ is negative the rotation is from right to left. In the case where $n$ is zero, the function returns $u$.

<a id="function-substitute-sgpfpelt-rngintelt-sgpfpelt-rngintelt"></a>
## `Substitute(u, f, n, v): SgpFPElt, RngIntElt, SgpFPElt, RngIntElt -> SgpFPElt`

Given words $u$ and $v$ belonging to a semigroup $S$, and non-negative integers $f$ and $n$, this function replaces the substring of $u$ of length $n$, starting at position $f$, by the word $v$. Thus, if $u = x_{i_1}\cdots x_{i_f}\cdots x_{i_{f+n-1}}\cdots x_{i_m}$ then the substring $x_{i_f}\cdots x_{i_{f+n-1}}$ is replaced by $v$. If $u$ and $v$ belong to a monoid $M$ and the function is invoked with $v =$ `Id(M)`, then the substring $x_{i_f}\cdots x_{i_{f+n-1}}$ of $u$ is deleted.

<a id="function-subword-sgpfpelt-rngintelt-rngintelt"></a>
## `Subword(u, f, n): SgpFPElt, RngIntElt, RngIntElt -> SgpFPElt`

The subword of the word $u$ comprising the $n$ consecutive letters commencing at the $f$-th letter of $u$.

<a id="function-elementtosequence-sgpfpelt"></a>
## `ElementToSequence(u): SgpFPElt -> [ SgpFPElt ]`

<a id="function-eltseq-sgpfpelt"></a>
## `Eltseq(u): SgpFPElt -> [ SgpFPElt ]`

The sequence obtained by decomposing $u$ into the indices of its constituent generators. Thus, if $u = x_{i_1}\ldots x_{i_m}$, then the sequence constructed by `ElementToSequence` is $[i_1,i_2, \ldots, i_m]$.
