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]

Converts a String.Slice into a Substring.Raw.

Instances For
    @[inline]

    Converts a Substring.Raw into a String.Slice, returning none if the substring is invalid.

    Instances For
      @[inline]

      Checks whether a substring is empty.

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

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

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

          Instances For
            @[export lean_substring_tostring]
            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.

              Instances For
                @[export lean_substring_get]
                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.

                  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.

                    Instances For
                      @[export lean_substring_prev]
                      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.

                        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.

                          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.

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

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

                                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.

                                  Instances For
                                    @[export lean_substring_drop]
                                    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.

                                      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.

                                        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.

                                          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.

                                            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.

                                              Instances For
                                                @[export lean_substring_extract]
                                                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.

                                                  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.

                                                    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.

                                                      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.

                                                        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.

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

                                                              Checks whether a substring contains the specified character.

                                                              Instances For
                                                                @[irreducible, specialize #[]]
                                                                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.

                                                                  Instances For
                                                                    @[export lean_substring_takewhile]
                                                                    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.

                                                                      Instances For
                                                                        @[irreducible, specialize #[]]
                                                                        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.

                                                                          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.

                                                                            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.

                                                                              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.

                                                                                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⟩
                                                                                  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. Underscores ({lit}_) are allowed as digit separators for readability, but cannot appear at the start, at the end, or consecutively.

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

                                                                                    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. Underscores ({lit}_) are allowed as digit separators and are ignored during parsing.

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

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

                                                                                        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.

                                                                                          Instances For
                                                                                            @[export lean_substring_beq]
                                                                                            Instances For
                                                                                              @[implicit_reducible]
                                                                                              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.

                                                                                              Instances For

                                                                                                Returns the longest common prefix of two substrings.

                                                                                                The returned substring uses the same underlying string as s.

                                                                                                Instances For

                                                                                                  Returns the longest common suffix of two substrings.

                                                                                                  The returned substring uses the same underlying string as s.

                                                                                                  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.

                                                                                                    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.

                                                                                                      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 :
                                                                                                        Instances For
                                                                                                          @[reducible, inline, deprecated Substring.Raw.bsize (since := "2025-11-16")]
                                                                                                          abbrev Substring.bsize :
                                                                                                          RawNat
                                                                                                          Instances For
                                                                                                            @[reducible, inline, deprecated Substring.Raw.toString (since := "2025-11-16")]
                                                                                                            Instances For
                                                                                                              @[reducible, inline, deprecated Substring.Raw.isEmpty (since := "2025-11-16")]
                                                                                                              abbrev Substring.isEmpty (ss : Raw) :
                                                                                                              Instances For
                                                                                                                @[reducible, inline, deprecated Substring.Raw.next (since := "2025-11-16")]
                                                                                                                Instances For
                                                                                                                  @[reducible, inline, deprecated Substring.Raw.prev (since := "2025-11-16")]
                                                                                                                  Instances For
                                                                                                                    @[reducible, inline, deprecated Substring.Raw.atEnd (since := "2025-11-16")]
                                                                                                                    Instances For
                                                                                                                      @[reducible, inline, deprecated Substring.Raw.beq (since := "2025-11-16")]
                                                                                                                      abbrev Substring.beq (ss1 ss2 : Raw) :
                                                                                                                      Instances For