Documentation

Init.Data.String.Substring

The Substring type #

This file contains API for Substring type, which is a legacy API that will be replaced by the safer variant String.Slice.

@[inline]

Checks whether a substring is empty.

A substring is empty if its start and end positions are the same.

Equations
Instances For
    @[export lean_substring_isempty]
    Equations
    Instances For
      @[inline]

      Copies the region of the underlying string pointed to by a substring into a fresh string.

      Equations
      Instances For
        @[inline]

        Returns the character at the given position in the substring.

        The position is relative to the substring, rather than the underlying string, and no bounds checking is performed with respect to the substring's end position. If the relative position is not a valid position in the underlying string, the fallback value (default : Char), which is 'A', is returned. Does not panic.

        Equations
        Instances For
          @[inline]

          Returns the next position in a substring after the given position. If the position is at the end of the substring, it is returned unmodified.

          Both the input position and the returned position are interpreted relative to the substring's start position, not the underlying string.

          Equations
          Instances For
            @[inline]

            Returns the previous position in a substring, just prior to the given position. If the position is at the beginning of the substring, it is returned unmodified.

            Both the input position and the returned position are interpreted relative to the substring's start position, not the underlying string.

            Equations
            Instances For

              Returns the position that's the specified number of characters forward from the given position in a substring. If the end position of the substring is reached, it is returned.

              Both the input position and the returned position are interpreted relative to the substring's start position, not the underlying string.

              Equations
              Instances For

                Returns the position that's the specified number of characters prior to the given position in a substring. If the start position of the substring is reached, it is returned.

                Both the input position and the returned position are interpreted relative to the substring's start position, not the underlying string.

                Equations
                Instances For
                  @[inline]

                  Returns the first character in the substring.

                  If the substring is empty, but the substring's start position is a valid position in the underlying string, then the character at the start position is returned. If the substring's start position is not a valid position in the string, the fallback value (default : Char), which is 'A', is returned. Does not panic.

                  Equations
                  Instances For
                    @[export lean_substring_front]
                    Equations
                    Instances For
                      @[inline]

                      Returns the substring-relative position of the first occurrence of c in s, or s.bsize if c doesn't occur.

                      Equations
                      Instances For
                        @[inline]

                        Removes the specified number of characters (Unicode code points) from the beginning of a substring by advancing its start position.

                        If the substring's end position is reached, the start position is not advanced past it.

                        Equations
                        • { str := s, startPos := b, stopPos := e }.drop x✝ = { str := s, startPos := ({ str := s, startPos := b, stopPos := e }.nextn x✝ 0).offsetBy b, stopPos := e }
                        Instances For
                          @[export lean_substring_drop]
                          Equations
                          Instances For
                            @[inline]

                            Removes the specified number of characters (Unicode code points) from the end of a substring by moving its end position towards its start position.

                            If the substring's start position is reached, the end position is not retracted past it.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              @[inline]

                              Retains only the specified number of characters (Unicode code points) at the beginning of a substring, by moving its end position towards its start position.

                              If the substring's start position is reached, the end position is not retracted past it.

                              Equations
                              • { str := s, startPos := b, stopPos := e }.take x✝ = { str := s, startPos := b, stopPos := ({ str := s, startPos := b, stopPos := e }.nextn x✝ 0).offsetBy b }
                              Instances For
                                @[inline]

                                Retains only the specified number of characters (Unicode code points) at the end of a substring, by moving its start position towards its end position.

                                If the substring's end position is reached, the start position is not advanced past it.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  @[inline]

                                  Checks whether a position in a substring is precisely equal to its ending position.

                                  The position is understood relative to the substring's starting position, rather than the underlying string's starting position.

                                  Equations
                                  Instances For
                                    @[inline]

                                    Returns the region of the substring delimited by the provided start and stop positions, as a substring. The positions are interpreted with respect to the substring's start position, rather than the underlying string.

                                    If the resulting substring is empty, then the resulting substring is a substring of the empty string "". Otherwise, the underlying string is that of the input substring with the beginning and end positions adjusted.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      def Substring.Raw.splitOn (s : Raw) (sep : String := " ") :

                                      Splits a substring s on occurrences of the separator string sep. The default separator is " ".

                                      When sep is empty, the result is [s]. When sep occurs in overlapping patterns, the first match is taken. There will always be exactly n+1 elements in the returned list if there were n non-overlapping matches of sep in the string. The separators are not included in the returned substrings, which are all substrings of s's string.

                                      Equations
                                      Instances For
                                        @[inline]
                                        def Substring.Raw.foldl {α : Type u} (f : αCharα) (init : α) (s : Raw) :
                                        α

                                        Folds a function over a substring from the left, accumulating a value starting with init. The accumulated value is combined with each character in order, using f.

                                        Equations
                                        Instances For
                                          @[inline]
                                          def Substring.Raw.foldr {α : Type u} (f : Charαα) (init : α) (s : Raw) :
                                          α

                                          Folds a function over a substring from the right, accumulating a value starting with init. The accumulated value is combined with each character in reverse order, using f.

                                          Equations
                                          Instances For
                                            @[inline]
                                            def Substring.Raw.any (s : Raw) (p : CharBool) :

                                            Checks whether the Boolean predicate p returns true for any character in a substring.

                                            Short-circuits at the first character for which p returns true.

                                            Equations
                                            • { str := s_1, startPos := b, stopPos := e }.any p = s_1.anyAux e p b
                                            Instances For
                                              @[inline]
                                              def Substring.Raw.all (s : Raw) (p : CharBool) :

                                              Checks whether the Boolean predicate p returns true for every character in a substring.

                                              Short-circuits at the first character for which p returns false.

                                              Equations
                                              Instances For
                                                @[export lean_substring_all]
                                                Equations
                                                Instances For
                                                  @[inline]

                                                  Checks whether a substring contains the specified character.

                                                  Equations
                                                  Instances For
                                                    @[irreducible, specialize #[]]
                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For
                                                      @[inline]

                                                      Retains only the longest prefix of a substring in which a Boolean predicate returns true for all characters by moving the substring's end position towards its start position.

                                                      Equations
                                                      Instances For
                                                        @[export lean_substring_takewhile]
                                                        Equations
                                                        Instances For
                                                          @[inline]

                                                          Removes the longest prefix of a substring in which a Boolean predicate returns true for all characters by moving the substring's start position. The start position is moved to the position of the first character for which the predicate returns false, or to the substring's end position if the predicate always returns true.

                                                          Equations
                                                          Instances For
                                                            @[irreducible, specialize #[]]
                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For
                                                              @[inline]

                                                              Retains only the longest suffix of a substring in which a Boolean predicate returns true for all characters by moving the substring's start position towards its end position.

                                                              Equations
                                                              Instances For
                                                                @[inline]

                                                                Removes the longest suffix of a substring in which a Boolean predicate returns true for all characters by moving the substring's end position. The end position is moved just after the position of the last character for which the predicate returns false, or to the substring's start position if the predicate always returns true.

                                                                Equations
                                                                Instances For
                                                                  @[inline]

                                                                  Removes leading whitespace from a substring by moving its start position to the first non-whitespace character, or to its end position if there is no non-whitespace character.

                                                                  “Whitespace” is defined as characters for which Char.isWhitespace returns true.

                                                                  Equations
                                                                  Instances For
                                                                    @[inline]

                                                                    Removes trailing whitespace from a substring by moving its end position to the last non-whitespace character, or to its start position if there is no non-whitespace character.

                                                                    “Whitespace” is defined as characters for which Char.isWhitespace returns true.

                                                                    Equations
                                                                    Instances For
                                                                      @[inline]

                                                                      Removes leading and trailing whitespace from a substring by first moving its start position to the first non-whitespace character, and then moving its end position to the last non-whitespace character.

                                                                      If the substring consists only of whitespace, then the resulting substring's start position is moved to its end position.

                                                                      “Whitespace” is defined as characters for which Char.isWhitespace returns true.

                                                                      Examples:

                                                                      • " red green blue ".toRawSubstring.trim.toString = "red green blue"
                                                                      • " red green blue ".toRawSubstring.trim.startPos = ⟨1⟩
                                                                      • " red green blue ".toRawSubstring.trim.stopPos = ⟨15⟩
                                                                      • " ".toRawSubstring.trim.startPos = ⟨5⟩
                                                                      Equations
                                                                      • One or more equations did not get rendered due to their size.
                                                                      Instances For
                                                                        @[inline]

                                                                        Checks whether the substring can be interpreted as the decimal representation of a natural number.

                                                                        A substring can be interpreted as a decimal natural number if it is not empty and all the characters in it are digits.

                                                                        Use Substring.toNat? to convert such a substring to a natural number.

                                                                        Equations
                                                                        Instances For

                                                                          Checks whether the substring can be interpreted as the decimal representation of a natural number, returning the number if it can.

                                                                          A substring can be interpreted as a decimal natural number if it is not empty and all the characters in it are digits.

                                                                          Use Substring.isNat to check whether the substring is such a substring.

                                                                          Equations
                                                                          Instances For

                                                                            Given a Substring, returns another one which has valid endpoints and represents the same substring according to Substring.toString. (Note, the substring may still be inverted, i.e. beginning greater than end.)

                                                                            Equations
                                                                            • One or more equations did not get rendered due to their size.
                                                                            Instances For
                                                                              def Substring.Raw.beq (ss1 ss2 : Raw) :

                                                                              Checks whether two substrings represent equal strings. Usually accessed via the == operator.

                                                                              Two substrings do not need to have the same underlying string or the same start and end positions; instead, they are equal if they contain the same sequence of characters.

                                                                              Equations
                                                                              Instances For
                                                                                @[export lean_substring_beq]
                                                                                Equations
                                                                                Instances For
                                                                                  def Substring.Raw.sameAs (ss1 ss2 : Raw) :

                                                                                  Checks whether two substrings have the same position and content.

                                                                                  The two substrings do not need to have the same underlying string for this check to succeed.

                                                                                  Equations
                                                                                  Instances For

                                                                                    Returns the longest common prefix of two substrings.

                                                                                    The returned substring uses the same underlying string as s.

                                                                                    Equations
                                                                                    Instances For

                                                                                      Returns the longest common suffix of two substrings.

                                                                                      The returned substring uses the same underlying string as s.

                                                                                      Equations
                                                                                      Instances For

                                                                                        If pre is a prefix of s, returns the remainder. Returns none otherwise.

                                                                                        The substring pre is a prefix of s if there exists a t : Substring such that s.toString = pre.toString ++ t.toString. If so, the result is the substring of s without the prefix.

                                                                                        Equations
                                                                                        Instances For

                                                                                          If suff is a suffix of s, returns the remainder. Returns none otherwise.

                                                                                          The substring suff is a suffix of s if there exists a t : Substring such that s.toString = t.toString ++ suff.toString. If so, the result the substring of s without the suffix.

                                                                                          Equations
                                                                                          Instances For
                                                                                            @[simp]
                                                                                            theorem Substring.Raw.prev_zero (s : Raw) :
                                                                                            s.prev 0 = 0
                                                                                            @[simp]
                                                                                            theorem Substring.Raw.prevn_zero (s : Raw) (n : Nat) :
                                                                                            s.prevn n 0 = 0
                                                                                            @[reducible, inline, deprecated Substring.Raw (since := "2025-11-16")]
                                                                                            abbrev Substring :
                                                                                            Equations
                                                                                            Instances For
                                                                                              @[reducible, inline, deprecated Substring.Raw.bsize (since := "2025-11-16")]
                                                                                              abbrev Substring.bsize :
                                                                                              RawNat
                                                                                              Equations
                                                                                              Instances For
                                                                                                @[reducible, inline, deprecated Substring.Raw.toString (since := "2025-11-16")]
                                                                                                Equations
                                                                                                Instances For
                                                                                                  @[reducible, inline, deprecated Substring.Raw.isEmpty (since := "2025-11-16")]
                                                                                                  abbrev Substring.isEmpty (ss : Raw) :
                                                                                                  Equations
                                                                                                  Instances For
                                                                                                    @[reducible, inline, deprecated Substring.Raw.next (since := "2025-11-16")]
                                                                                                    Equations
                                                                                                    Instances For
                                                                                                      @[reducible, inline, deprecated Substring.Raw.prev (since := "2025-11-16")]
                                                                                                      Equations
                                                                                                      Instances For
                                                                                                        @[reducible, inline, deprecated Substring.Raw.atEnd (since := "2025-11-16")]
                                                                                                        Equations
                                                                                                        Instances For
                                                                                                          @[reducible, inline, deprecated Substring.Raw.beq (since := "2025-11-16")]
                                                                                                          abbrev Substring.beq (ss1 ss2 : Raw) :
                                                                                                          Equations
                                                                                                          Instances For