Documentation

Init.Data.String.Search

@[inline]
def String.replace {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] {α : Type u_1} [ToSlice α] (s : String) (pattern : ρ) [Slice.Pattern.ToForwardSearcher pattern σ] (replacement : α) :

Constructs a new string obtained by replacing all occurrences of pattern with replacement in s.

This function is generic over all currently supported patterns. The replacement may be a String or a String.Slice.

Examples:

  • "red green blue".replace 'e' "" = "rd grn blu"

  • "red green blue".replace (fun c => c == 'u' || c == 'e') "" = "rd grn bl"

  • "red green blue".replace "e" "" = "rd grn blu"

  • "red green blue".replace "ee" "E" = "red grEn blue"

  • "red green blue".replace "e" "E" = "rEd grEEn bluE"

  • "aaaaa".replace "aa" "b" = "bba"

  • "abc".replace "" "k" = "kakbkck"

Equations
Instances For
    @[inline]
    def String.Slice.Pos.find? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] {s : Slice} (pos : s.Pos) (pattern : ρ) [Pattern.ToForwardSearcher pattern σ] :

    Finds the position of the first match of the pattern pattern in after the position pos. If there is no match none is returned.

    This function is generic over all currently supported patterns.

    Examples:

    • ("coffee tea water".toSlice.startPos.find? Char.isWhitespace).map (·.get!) == some ' '

    • ("tea".toSlice.pos ⟨1⟩ (by decide)).find? (fun (c : Char) => c == 't') == none

    Equations
    Instances For
      @[inline]
      def String.Slice.Pos.find {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] {s : Slice} (pos : s.Pos) (pattern : ρ) [Pattern.ToForwardSearcher pattern σ] :
      s.Pos

      Finds the position of the first match of the pattern pattern in after the position pos. If there is no match s.endPos is returned.

      This function is generic over all currently supported patterns.

      Examples:

      • ("coffee tea water".toSlice.startPos.find Char.isWhitespace).get! == ' '

      • ("tea".toSlice.pos ⟨1⟩ (by decide)).find (fun (c : Char) => c == 't') == "tea".toSlice.endPos

      Equations
      Instances For
        @[inline]
        def String.Pos.find? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] {s : String} (pos : s.Pos) (pattern : ρ) [Slice.Pattern.ToForwardSearcher pattern σ] :

        Finds the position of the first match of the pattern pattern in after the position pos. If there is no match none is returned.

        This function is generic over all currently supported patterns.

        Examples:

        • ("coffee tea water".startPos.find? Char.isWhitespace).map (·.get!) == some ' '

        • ("tea".pos ⟨1⟩ (by decide)).find? (fun (c : Char) => c == 't') == none

        Equations
        Instances For
          @[inline]
          def String.Pos.find {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] {s : String} (pos : s.Pos) (pattern : ρ) [Slice.Pattern.ToForwardSearcher pattern σ] :
          s.Pos

          Finds the position of the first match of the pattern pattern in after the position pos. If there is no match s.endPos is returned.

          This function is generic over all currently supported patterns.

          Examples:

          • ("coffee tea water".startPos.find Char.isWhitespace).get! == ' '

          • ("tea".pos ⟨1⟩ (by decide)).find (fun (c : Char) => c == 't') == "tea".endPos

          Equations
          Instances For
            @[inline]
            def String.find? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] (s : String) (pattern : ρ) [Slice.Pattern.ToForwardSearcher pattern σ] :

            Finds the position of the first match of the pattern pattern in a string s. If there is no match none is returned.

            This function is generic over all currently supported patterns.

            Examples:

            • ("coffee tea water".find? Char.isWhitespace).map (·.get!) == some ' '

            • "tea".find? (fun (c : Char) => c == 'X') == none

            • ("coffee tea water".find? "tea").map (·.get!) == some 't'

            Equations
            Instances For
              @[inline]
              def String.find {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] (s : String) (pattern : ρ) [Slice.Pattern.ToForwardSearcher pattern σ] :
              s.Pos

              Finds the position of the first match of the pattern pattern in a slice s. If there is no match s.endPos is returned.

              This function is generic over all currently supported patterns.

              Examples:

              • ("coffee tea water".find Char.isWhitespace).get! == ' '

              • "tea".find (fun (c : Char) => c == 'X') == "tea".endPos

              • ("coffee tea water".find "tea").get! == 't'

              Equations
              Instances For
                @[inline]
                def String.Slice.Pos.revFind? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] {s : Slice} (pos : s.Pos) (pattern : ρ) [Pattern.ToBackwardSearcher pattern σ] :

                Finds the position of the first match of the pattern pattern in a slice s that is strictly before pos. If there is no such match none is returned.

                This function is generic over all currently supported patterns except String/String.Slice.

                Examples:

                • (("abc".toSlice.endPos.prev (by decide)).revFind? Char.isAlpha).map (·.get!) == some 'b'

                • "abc".toSlice.startPos.revFind? Char.isAlpha == none

                Equations
                Instances For
                  @[inline]
                  def String.Pos.revFind? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] {s : String} (pos : s.Pos) (pattern : ρ) [Slice.Pattern.ToBackwardSearcher pattern σ] :

                  Finds the position of the first match of the pattern pattern in a slice s that is strictly before pos. If there is no such match none is returned.

                  This function is generic over all currently supported patterns except String/String.Slice.

                  Examples:

                  Equations
                  Instances For
                    @[inline]
                    def String.revFind? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] (s : String) (pattern : ρ) [Slice.Pattern.ToBackwardSearcher pattern σ] :

                    Finds the position of the first match of the pattern pattern in a string, starting from the end of the slice and traversing towards the start. If there is no match none is returned.

                    This function is generic over all currently supported patterns except String/String.Slice.

                    Examples:

                    • ("coffee tea water".toSlice.revFind? Char.isWhitespace).map (·.get!) == some ' '

                    • "tea".toSlice.revFind? (fun (c : Char) => c == 'X') == none

                    Equations
                    Instances For
                      @[export lean_string_posof]
                      Equations
                      Instances For
                        @[deprecated String.Pos.find (since := "2025-11-19")]
                        def String.findAux (s : String) (p : CharBool) (stopPos pos : Pos.Raw) :
                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[deprecated String.Pos.find (since := "2025-11-19")]
                          def String.posOfAux (s : String) (c : Char) (stopPos pos : Pos.Raw) :
                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[deprecated String.find (since := "2025-11-19")]
                            def String.posOf (s : String) (c : Char) :
                            Equations
                            Instances For
                              @[deprecated String.Pos.revFind? (since := "2025-11-19")]
                              Equations
                              Instances For
                                @[deprecated String.revFind? (since := "2025-11-19")]
                                Equations
                                Instances For
                                  @[deprecated String.Pos.revFind? (since := "2025-11-19")]
                                  def String.revFindAux (s : String) (p : CharBool) (pos : Pos.Raw) :
                                  Equations
                                  Instances For
                                    @[deprecated String.revFind? (since := "2025-11-19")]
                                    Equations
                                    Instances For
                                      @[deprecated String.Pos.revFind? (since := "2025-11-19")]

                                      Returns the position of the beginning of the line that contains the position pos.

                                      Lines are ended by '\n', and the returned position is either 0 : String.Pos.Raw or immediately after a '\n' character.

                                      Equations
                                      Instances For
                                        @[inline]
                                        def String.split {ρ : Type} {σ : SliceType} (s : String) (pat : ρ) [Slice.Pattern.ToForwardSearcher pat σ] :

                                        Splits a string at each subslice that matches the pattern pat.

                                        The subslices that matched the pattern are not included in any of the resulting subslices. If multiple subslices in a row match the pattern, the resulting list will contain empty strings.

                                        This function is generic over all currently supported patterns.

                                        Examples:

                                        • ("coffee tea water".split Char.isWhitespace).toList == ["coffee".toSlice, "tea".toSlice, "water".toSlice]

                                        • ("coffee tea water".split ' ').toList == ["coffee".toSlice, "tea".toSlice, "water".toSlice]

                                        • ("coffee tea water".split " tea ").toList == ["coffee".toSlice, "water".toSlice]

                                        • ("ababababa".split "aba").toList == ["coffee".toSlice, "water".toSlice]

                                        • ("baaab".split "aa").toList == ["b".toSlice, "ab".toSlice]

                                        Equations
                                        Instances For
                                          @[inline]
                                          def String.splitInclusive {ρ : Type} {σ : SliceType} (s : String) (pat : ρ) [Slice.Pattern.ToForwardSearcher pat σ] :

                                          Splits a string at each subslice that matches the pattern pat. Unlike split the matched subslices are included at the end of each subslice.

                                          This function is generic over all currently supported patterns.

                                          Examples:

                                          • ("coffee tea water".splitInclusive Char.isWhitespace).toList == ["coffee ".toSlice, "tea ".toSlice, "water".toSlice]

                                          • ("coffee tea water".splitInclusive ' ').toList == ["coffee ".toSlice, "tea ".toSlice, "water".toSlice]

                                          • ("coffee tea water".splitInclusive " tea ").toList == ["coffee tea ".toSlice, "water".toSlice]

                                          • ("baaab".splitInclusive "aa").toList == ["baa".toSlice, "ab".toSlice]

                                          Equations
                                          Instances For
                                            @[deprecated String.Slice.foldl (since := "2025-11-20")]
                                            def String.foldlAux {α : Type u} (f : αCharα) (s : String) (stopPos i : Pos.Raw) (a : α) :
                                            α
                                            Equations
                                            Instances For
                                              @[inline]
                                              def String.foldl {α : Type u} (f : αCharα) (init : α) (s : String) :
                                              α

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

                                              Examples:

                                              • "coffee tea water".foldl (fun n c => if c.isWhitespace then n + 1 else n) 0 = 2

                                              • "coffee tea and water".foldl (fun n c => if c.isWhitespace then n + 1 else n) 0 = 3

                                              • "coffee tea water".foldl (·.push ·) "" = "coffee tea water"

                                              Equations
                                              Instances For
                                                @[export lean_string_foldl]
                                                Equations
                                                Instances For
                                                  @[deprecated String.Slice.foldr (since := "2025-11-25")]
                                                  def String.foldrAux {α : Type u} (f : Charαα) (a : α) (s : String) (i begPos : Pos.Raw) :
                                                  α
                                                  Equations
                                                  Instances For
                                                    @[inline]
                                                    def String.foldr {α : Type u} (f : Charαα) (init : α) (s : String) :
                                                    α

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

                                                    Examples:

                                                    • "coffee tea water".foldr (fun c n => if c.isWhitespace then n + 1 else n) 0 = 2

                                                    • "coffee tea and water".foldr (fun c n => if c.isWhitespace then n + 1 else n) 0 = 3

                                                    • "coffee tea water".foldr (fun c s => s.push c) "" = "retaw aet eeffoc"

                                                    Equations
                                                    Instances For
                                                      @[deprecated String.Slice.any (since := "2025-11-25")]
                                                      def String.anyAux (s : String) (stopPos : Pos.Raw) (p : CharBool) (i : Pos.Raw) :
                                                      Equations
                                                      Instances For
                                                        @[inline]
                                                        def String.contains {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] (s : String) (pat : ρ) [Slice.Pattern.ToForwardSearcher pat σ] :

                                                        Checks whether a string has a match of the pattern pat anywhere.

                                                        This function is generic over all currently supported patterns.

                                                        Examples:

                                                        Equations
                                                        Instances For
                                                          @[export lean_string_contains]
                                                          Equations
                                                          Instances For
                                                            @[inline]
                                                            def String.any {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterators.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.Iterators.IteratorLoop (σ s) Id Id] (s : String) (pat : ρ) [Slice.Pattern.ToForwardSearcher pat σ] :

                                                            Checks whether a string has a match of the pattern pat anywhere.

                                                            This function is generic over all currently supported patterns.

                                                            Examples:

                                                            Equations
                                                            Instances For
                                                              @[export lean_string_any]
                                                              Equations
                                                              Instances For
                                                                @[inline]
                                                                def String.all {ρ : Type} (s : String) (pat : ρ) [Slice.Pattern.ForwardPattern pat] :

                                                                Checks whether a slice only consists of matches of the pattern pat.

                                                                Short-circuits at the first pattern mis-match.

                                                                This function is generic over all currently supported patterns.

                                                                Examples:

                                                                Equations
                                                                Instances For
                                                                  @[inline]

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

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

                                                                  Use toNat? or toNat! to convert such a slice to a natural number.

                                                                  Examples:

                                                                  Equations
                                                                  Instances For
                                                                    @[inline]

                                                                    Interprets a string as the decimal representation of a natural number, returning it. Returns none if the slice does not contain a decimal natural number.

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

                                                                    Use isNat to check whether toNat? would return some. toNat! is an alternative that panics instead of returning none when the slice is not a natural number.

                                                                    Examples:

                                                                    Equations
                                                                    Instances For
                                                                      @[inline]

                                                                      Interprets a string as the decimal representation of a natural number, returning it. Panics if the slice does not contain a decimal natural number.

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

                                                                      Use isNat to check whether toNat! would return a value. toNat? is a safer alternative that returns none instead of panicking when the string is not a natural number.

                                                                      Examples:

                                                                      Equations
                                                                      Instances For
                                                                        @[inline]

                                                                        Interprets a string as the decimal representation of an integer, returning it. Returns none if the string does not contain a decimal integer.

                                                                        A string can be interpreted as a decimal integer if it only consists of at least one decimal digit and optionally - in front. Leading + characters are not allowed.

                                                                        Use String.isInt to check whether String.toInt? would return some. String.toInt! is an alternative that panics instead of returning none when the string is not an integer.

                                                                        Examples:

                                                                        Equations
                                                                        Instances For
                                                                          @[inline]

                                                                          Checks whether the string can be interpreted as the decimal representation of an integer.

                                                                          A string can be interpreted as a decimal integer if it only consists of at least one decimal digit and optionally - in front. Leading + characters are not allowed.

                                                                          Use String.toInt? or String.toInt! to convert such a string to an integer.

                                                                          Examples:

                                                                          Equations
                                                                          Instances For
                                                                            @[inline]

                                                                            Interprets a string as the decimal representation of an integer, returning it. Panics if the string does not contain a decimal integer.

                                                                            A string can be interpreted as a decimal integer if it only consists of at least one decimal digit and optionally - in front. Leading + characters are not allowed.

                                                                            Use String.isInt to check whether String.toInt! would return a value. String.toInt? is a safer alternative that returns none instead of panicking when the string is not an integer.

                                                                            Examples:

                                                                            Equations
                                                                            Instances For
                                                                              @[inline]

                                                                              Returns the first character in s. If s is empty, returns none.

                                                                              Examples:

                                                                              Equations
                                                                              Instances For
                                                                                @[inline]

                                                                                Returns the first character in s. If s = "", returns (default : Char).

                                                                                Examples:

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

                                                                                    Returns the last character in s. If s is empty, returns none.

                                                                                    Examples:

                                                                                    Equations
                                                                                    Instances For
                                                                                      @[inline]

                                                                                      Returns the last character in s. If s = "", returns (default : Char).

                                                                                      Examples:

                                                                                      • "abc".back = 'c'

                                                                                      • "".back = (default : Char)

                                                                                      Equations
                                                                                      Instances For
                                                                                        @[inline]

                                                                                        Creates an iterator over all valid positions within s.

                                                                                        Examples

                                                                                        • ("abc".positions.map (fun ⟨p, h⟩ => p.get h) |>.toList) = ['a', 'b', 'c']

                                                                                        • ("abc".positions.map (·.val.offset.byteIdx) |>.toList) = [0, 1, 2]

                                                                                        • ("ab∀c".positions.map (fun ⟨p, h⟩ => p.get h) |>.toList) = ['a', 'b', '∀', 'c']

                                                                                        • ("ab∀c".positions.map (·.val.offset.byteIdx) |>.toList) = [0, 1, 2, 5]

                                                                                        Equations
                                                                                        Instances For
                                                                                          @[inline]

                                                                                          Creates an iterator over all characters (Unicode code points) in s.

                                                                                          Examples:

                                                                                          • "abc".chars.toList = ['a', 'b', 'c']

                                                                                          • "ab∀c".chars.toList = ['a', 'b', '∀', 'c']

                                                                                          Equations
                                                                                          Instances For
                                                                                            @[inline]

                                                                                            Creates an iterator over all valid positions within s, starting from the last valid position and iterating towards the first one.

                                                                                            Examples

                                                                                            • ("abc".revPositions.map (fun ⟨p, h⟩ => p.get h) |>.toList) = ['c', 'b', 'a']

                                                                                            • ("abc".revPositions.map (·.val.offset.byteIdx) |>.toList) = [2, 1, 0]

                                                                                            • ("ab∀c".revPositions.map (fun ⟨p, h⟩ => p.get h) |>.toList) = ['c', '∀', 'b', 'a']

                                                                                            • ("ab∀c".toSlice.revPositions.map (·.val.offset.byteIdx) |>.toList) = [5, 2, 1, 0]

                                                                                            Equations
                                                                                            Instances For
                                                                                              @[inline]

                                                                                              Creates an iterator over all characters (Unicode code points) in s, starting from the end of the slice and iterating towards the start.

                                                                                              Example:

                                                                                              • "abc".revChars.toList = ['c', 'b', 'a']

                                                                                              • "ab∀c".revChars.toList = ['c', '∀', 'b', 'a']

                                                                                              Equations
                                                                                              Instances For
                                                                                                @[inline]

                                                                                                Creates an iterator over all bytes in s.

                                                                                                Examples:

                                                                                                • "abc".byteIterator.toList = [97, 98, 99]

                                                                                                • "ab∀c".byteIterator.toList = [97, 98, 226, 136, 128, 99]

                                                                                                Equations
                                                                                                Instances For
                                                                                                  @[inline]

                                                                                                  Creates an iterator over all bytes in s, starting from the last one and iterating towards the first one.

                                                                                                  Examples:

                                                                                                  • "abc".revBytes.toList = [99, 98, 97]

                                                                                                  • "ab∀c".revBytes.toList = [99, 128, 136, 226, 98, 97]

                                                                                                  Equations
                                                                                                  Instances For

                                                                                                    Creates an iterator over all lines in s with the line ending characters \r\n or \n being stripped.

                                                                                                    Examples:

                                                                                                    • "foo\r\nbar\n\nbaz\n".lines.toList == ["foo".toSlice, "bar".toSlice, "".toSlice, "baz".toSlice]

                                                                                                    • "foo\r\nbar\n\nbaz".lines.toList == ["foo".toSlice, "bar".toSlice, "".toSlice, "baz".toSlice]

                                                                                                    • "foo\r\nbar\n\nbaz\r".lines.toList == ["foo".toSlice, "bar".toSlice, "".toSlice, "baz\r".toSlice]

                                                                                                    Equations
                                                                                                    Instances For