Documentation

Init.Data.String.Search

@[inline]
def String.replace {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.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"

Instances For
    @[inline]
    def String.Slice.Pos.find? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.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

    Instances For
      @[inline]
      def String.Slice.Pos.find {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.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

      Instances For
        @[inline]
        def String.Pos.find? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.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

        Instances For
          @[inline]
          def String.Pos.find {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.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

          Instances For
            @[inline]
            def String.find? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.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'

            Instances For
              @[inline]
              def String.find {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.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'

              Instances For
                @[inline]
                def String.Slice.Pos.revFind? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.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

                Instances For
                  @[inline]
                  def String.Pos.revFind? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.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:

                  Instances For
                    @[inline]
                    def String.revFind? {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.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

                    Instances For
                      @[export lean_string_posof]
                      Instances For
                        @[deprecated String.Pos.find (since := "2025-11-19")]
                        def String.findAux (s : String) (p : CharBool) (stopPos pos : Pos.Raw) :
                        Instances For
                          @[deprecated String.Pos.find (since := "2025-11-19")]
                          def String.posOfAux (s : String) (c : Char) (stopPos pos : Pos.Raw) :
                          Instances For
                            @[deprecated String.find (since := "2025-11-19")]
                            def String.posOf (s : String) (c : Char) :
                            Instances For
                              @[deprecated String.Pos.revFind? (since := "2025-11-19")]
                              Instances For
                                @[deprecated String.revFind? (since := "2025-11-19")]
                                Instances For
                                  @[deprecated String.Pos.revFind? (since := "2025-11-19")]
                                  def String.revFindAux (s : String) (p : CharBool) (pos : Pos.Raw) :
                                  Instances For
                                    @[deprecated String.revFind? (since := "2025-11-19")]
                                    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.

                                      Instances For
                                        @[inline]
                                        def String.split {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] (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]

                                        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]

                                          Instances For
                                            @[deprecated String.Slice.foldl (since := "2025-11-20")]
                                            def String.foldlAux {α : Type u} (f : αCharα) (s : String) (stopPos i : Pos.Raw) (a : α) :
                                            α
                                            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"

                                              Instances For
                                                @[export lean_string_foldl]
                                                Instances For
                                                  @[deprecated String.Slice.foldr (since := "2025-11-25")]
                                                  def String.foldrAux {α : Type u} (f : Charαα) (a : α) (s : String) (i begPos : Pos.Raw) :
                                                  α
                                                  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"

                                                    Instances For
                                                      @[deprecated String.Slice.any (since := "2025-11-25")]
                                                      def String.anyAux (s : String) (stopPos : Pos.Raw) (p : CharBool) (i : Pos.Raw) :
                                                      Instances For
                                                        @[inline]
                                                        def String.contains {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.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:

                                                        Instances For
                                                          @[export lean_string_contains]
                                                          Instances For
                                                            @[inline]
                                                            def String.any {ρ : Type} {σ : SliceType} [(s : Slice) → Std.Iterator (σ s) Id (Slice.Pattern.SearchStep s)] [(s : Slice) → Std.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:

                                                            Instances For
                                                              @[export lean_string_any]
                                                              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:

                                                                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:

                                                                  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:

                                                                    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:

                                                                      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:

                                                                        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:

                                                                          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:

                                                                            Instances For
                                                                              @[inline]

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

                                                                              Examples:

                                                                              Instances For
                                                                                @[inline]

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

                                                                                Examples:

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

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

                                                                                    Examples:

                                                                                    Instances For
                                                                                      @[inline]

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

                                                                                      Examples:

                                                                                      • "abc".back = 'c'

                                                                                      • "".back = (default : Char)

                                                                                      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]

                                                                                        Instances For