Documentation

Init.Data.String.TakeDrop

@[inline]
def String.drop (s : String) (n : Nat) :

Returns a String.Slice obtained by removing the specified number of characters (Unicode code points) from the start of the string.

If n is greater than s.toList.length, returns an empty slice.

This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

Examples:

  • "red green blue".drop 4 == "green blue".toSlice

  • "red green blue".drop 10 == "blue".toSlice

  • "red green blue".drop 50 == "".toSlice

  • "مرحبا بالعالم".drop 3 == "با بالعالم".toSlice

Equations
Instances For
    @[export lean_string_drop]
    Equations
    Instances For
      @[inline]
      def String.dropEnd (s : String) (n : Nat) :

      Returns a String.Slice obtained by removing the specified number of characters (Unicode code points) from the end of the string.

      If n is greater than s.toList.length, returns an empty slice.

      This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

      Examples:

      • "red green blue".dropEnd 5 == "red green".toSlice

      • "red green blue".dropEnd 11 == "red".toSlice

      • "red green blue".dropEnd 50 == "".toSlice

      • "مرحبا بالعالم".dropEnd 3 == "مرحبا بالع".toSlice

      Equations
      Instances For
        @[deprecated String.dropEnd (since := "2025-11-14")]
        Equations
        Instances For
          @[deprecated String.Slice.dropEnd (since := "2025-11-20")]
          Equations
          Instances For
            @[export lean_string_dropright]
            Equations
            Instances For
              @[inline]
              def String.take (s : String) (n : Nat) :

              Returns a String.Slice that contains the first n characters (Unicode code points) of s.

              If n is greater than s.toList.length, returns s.toSlice.

              This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

              Examples:

              • "red green blue".take 3 == "red".toSlice

              • "red green blue".take 1 == "r".toSlice

              • "red green blue".take 0 == "".toSlice

              • "red green blue".take 100 == "red green blue".toSlice

              • "مرحبا بالعالم".take 5 == "مرحبا".toSlice

              Equations
              Instances For
                @[inline]
                def String.takeEnd (s : String) (n : Nat) :

                Returns a String.Slice that contains the last n characters (Unicode code points) of s.

                If n is greater than s.toList.length, returns s.toSlice.

                This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

                Examples:

                • "red green blue".takeEnd 4 == "blue".toSlice

                • "red green blue".takeEnd 1 == "e".toSlice

                • "red green blue".takeEnd 0 == "".toSlice

                • "red green blue".takeEnd 100 == "red green blue".toSlice

                • "مرحبا بالعالم".takeEnd 5 == "لعالم".toSlice

                Equations
                Instances For
                  @[deprecated String.takeEnd (since := "2025-11-14")]
                  Equations
                  Instances For
                    @[deprecated String.Slice.takeEnd (since := "2025-11-20")]
                    Equations
                    Instances For
                      @[inline]

                      Creates a string slice that contains the longest prefix of s in which pat matched (potentially repeatedly).

                      This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

                      This function is generic over all currently supported patterns.

                      Examples:

                      Equations
                      Instances For
                        @[inline]

                        Creates a string slice by removing the longest prefix from s in which pat matched (potentially repeatedly).

                        This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

                        This function is generic over all currently supported patterns.

                        Examples:

                        • "red green blue".dropWhile Char.isLower == " green blue".toSlice

                        • "red green blue".dropWhile 'r' == "ed green blue".toSlice

                        • "red red green blue".dropWhile "red " == "green blue".toSlice

                        • "red green blue".dropWhile (fun (_ : Char) => true) == "".toSlice

                        Equations
                        Instances For
                          @[inline]

                          Creates a string slice that contains the longest suffix of s in which pat matched (potentially repeatedly).

                          This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

                          This function is generic over all currently supported patterns.

                          Examples:

                          Equations
                          Instances For
                            @[deprecated String.takeEndWhile (since := "2025-11-17")]
                            Equations
                            Instances For
                              @[deprecated String.Slice.takeEndWhile (since := "2025-11-20")]
                              Equations
                              Instances For
                                @[inline]

                                Creates a new string by removing the longest suffix from s in which pat matches (potentially repeatedly).

                                This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

                                This function is generic over all currently supported patterns.

                                Examples:

                                Equations
                                Instances For
                                  @[deprecated String.dropEndWhile (since := "2025-11-17")]
                                  Equations
                                  Instances For
                                    @[deprecated String.Slice.dropEndWhile (since := "2025-11-20")]
                                    Equations
                                    Instances For
                                      @[inline]

                                      If pat matches a prefix of s, returns the position at the start of the remainder. Returns none otherwise.

                                      This function is generic over all currently supported patterns.

                                      Equations
                                      Instances For
                                        @[inline]

                                        Returns the position after the longest prefix of s for which pat matches (potentially repeatedly).

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

                                          Checks whether a string 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]
                                            def String.revAll {ρ : Type} (s : String) (pat : ρ) [Slice.Pattern.BackwardPattern pat] :

                                            Checks whether a string only consists of matches of the pattern pat, starting from the back of the string.

                                            Short-circuits at the first pattern mis-match.

                                            This function is generic over all currently supported patterns.

                                            For many types of patterns, this function can be expected to return the same result as String.all. If mismatches are expected to occur close to the end of the string, this function might be more efficient.

                                            For some types of patterns, this function will return a different result than String.all. Consider, for example, a pattern that matches the longest string at the given position that matches the regular expression "a|aa|ab". Then, given the input string "aab", performing String.all will greedily match the prefix "aa" and then get stuck on the remainder "b", causing it to return false. On the other hand, String.revAll will match the suffix "ab" and then match the remainder "a", so it will return true.

                                            Examples:

                                            Equations
                                            Instances For
                                              @[inline]
                                              def String.Pos.skip? {ρ : Type} {s : String} (pos : s.Pos) (pat : ρ) [Slice.Pattern.ForwardPattern pat] :

                                              If pat matches at pos, returns the position after the end of the match. Returns none otherwise.

                                              This function is generic over all currently supported patterns.

                                              Equations
                                              Instances For
                                                @[inline]
                                                def String.Pos.skipWhile {ρ : Type} {s : String} (pos : s.Pos) (pat : ρ) [Slice.Pattern.ForwardPattern pat] :
                                                s.Pos

                                                Advances pos as long as pat matches.

                                                Equations
                                                Instances For
                                                  @[inline]

                                                  Checks whether the first string (s) begins with the pattern (pat).

                                                  String.isPrefixOf is a version that takes the potential prefix before the string.

                                                  Examples:

                                                  Equations
                                                  Instances For
                                                    @[inline]

                                                    Checks whether the second string (s) begins with a prefix (p).

                                                    This function is generic over all currently supported patterns.

                                                    String.startsWith is a version that takes the potential prefix after the string.

                                                    Examples:

                                                    Equations
                                                    Instances For
                                                      @[export lean_string_isprefixof]
                                                      Equations
                                                      Instances For
                                                        @[inline]
                                                        def String.endsWith {ρ : Type} (s : String) (pat : ρ) [Slice.Pattern.BackwardPattern pat] :

                                                        Checks whether the string (s) ends with the pattern (pat).

                                                        This function is generic over all currently supported patterns.

                                                        Examples:

                                                        Equations
                                                        Instances For
                                                          @[inline]

                                                          If pat matches a suffix of s, returns the position at the beginning of the suffix. Returns none otherwise.

                                                          This function is generic over all currently supported patterns.

                                                          Equations
                                                          Instances For
                                                            @[inline]

                                                            Returns the position at the start of the longest suffix of s for which pat matches (potentially repeatedly).

                                                            Equations
                                                            Instances For
                                                              @[inline]
                                                              def String.Pos.revSkip? {ρ : Type} {s : String} (pos : s.Pos) (pat : ρ) [Slice.Pattern.BackwardPattern pat] :

                                                              If pat matches at pos, returns the position after the end of the match. Returns none otherwise.

                                                              This function is generic over all currently supported patterns.

                                                              Equations
                                                              Instances For
                                                                @[inline]
                                                                def String.Pos.revSkipWhile {ρ : Type} {s : String} (pos : s.Pos) (pat : ρ) [Slice.Pattern.BackwardPattern pat] :
                                                                s.Pos

                                                                Rewinds pos as long as pat matches.

                                                                Equations
                                                                Instances For
                                                                  @[inline]

                                                                  Removes trailing whitespace from a string by returning a slice whose end position is the last non-whitespace character, or the start position if there is no non-whitespace character.

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

                                                                  Examples:

                                                                  Equations
                                                                  Instances For
                                                                    @[deprecated String.trimAsciiEnd (since := "2025-11-17")]
                                                                    Equations
                                                                    Instances For
                                                                      @[deprecated String.Slice.trimAsciiEnd (since := "2025-11-20")]
                                                                      Equations
                                                                      Instances For
                                                                        @[inline]

                                                                        Removes leading whitespace from a string by returning a slice whose start position is the first non-whitespace character, or the end position if there is no non-whitespace character.

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

                                                                        Examples:

                                                                        Equations
                                                                        Instances For
                                                                          @[deprecated String.trimAsciiStart (since := "2025-11-17")]
                                                                          Equations
                                                                          Instances For
                                                                            @[deprecated String.Slice.trimAsciiStart (since := "2025-11-20")]
                                                                            Equations
                                                                            Instances For
                                                                              @[inline]

                                                                              Removes leading and trailing whitespace from a string.

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

                                                                              Examples:

                                                                              Equations
                                                                              Instances For
                                                                                @[deprecated String.trimAscii (since := "2025-11-17")]
                                                                                Equations
                                                                                Instances For
                                                                                  @[deprecated String.Slice.trimAscii (since := "2025-11-20")]
                                                                                  Equations
                                                                                  Instances For
                                                                                    @[export lean_string_trim]
                                                                                    Equations
                                                                                    Instances For
                                                                                      @[inline]
                                                                                      def String.Pos.Raw.nextWhile (s : String) (p : CharBool) (i : Raw) :

                                                                                      Repeatedly increments a position in a string, as if by String.Pos.Raw.next, while the predicate p returns true for the character at the position. Stops incrementing at the end of the string or when p returns false for the current character.

                                                                                      Examples:

                                                                                      Equations
                                                                                      Instances For
                                                                                        @[reducible, inline, deprecated String.Pos.Raw.nextWhile (since := "2025-10-10")]
                                                                                        abbrev String.nextWhile (s : String) (p : CharBool) (i : Pos.Raw) :
                                                                                        Equations
                                                                                        Instances For
                                                                                          @[export lean_string_nextwhile]
                                                                                          Equations
                                                                                          Instances For
                                                                                            @[inline]
                                                                                            def String.Pos.Raw.nextUntil (s : String) (p : CharBool) (i : Raw) :

                                                                                            Repeatedly increments a position in a string, as if by String.Pos.Raw.next, while the predicate p returns false for the character at the position. Stops incrementing at the end of the string or when p returns true for the current character.

                                                                                            Examples:

                                                                                            Equations
                                                                                            Instances For
                                                                                              @[deprecated String.Pos.Raw.nextUntil (since := "2025-10-10")]
                                                                                              def String.nextUntil (s : String) (p : CharBool) (i : Pos.Raw) :
                                                                                              Equations
                                                                                              Instances For

                                                                                                If pat matches a prefix of s, returns the remainder. Returns none otherwise.

                                                                                                Use String.dropPrefix to return the slice unchanged when pat does not match a prefix.

                                                                                                This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

                                                                                                This function is generic over all currently supported patterns.

                                                                                                Examples:

                                                                                                Equations
                                                                                                Instances For

                                                                                                  If pat matches a suffix of s, returns the remainder. Returns none otherwise.

                                                                                                  Use String.dropSuffix to return the slice unchanged when pat does not match a suffix.

                                                                                                  This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

                                                                                                  This function is generic over all currently supported patterns.

                                                                                                  Examples:

                                                                                                  Equations
                                                                                                  Instances For

                                                                                                    If pat matches a prefix of s, returns the remainder. Returns s unmodified otherwise.

                                                                                                    Use String.dropPrefix? to return none when pat does not match a prefix.

                                                                                                    This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

                                                                                                    This function is generic over all currently supported patterns.

                                                                                                    Examples:

                                                                                                    Equations
                                                                                                    Instances For
                                                                                                      @[deprecated String.dropPrefix (since := "2025-11-17")]
                                                                                                      Equations
                                                                                                      Instances For
                                                                                                        @[deprecated String.Slice.dropPrefix (since := "2025-11-20")]
                                                                                                        Equations
                                                                                                        Instances For

                                                                                                          If pat matches a suffix of s, returns the remainder. Returns s unmodified otherwise.

                                                                                                          Use String.dropSuffix? to return none when pat does not match a prefix.

                                                                                                          This is a cheap operation because it does not allocate a new string to hold the result. To convert the result into a string, use String.Slice.copy.

                                                                                                          This function is generic over all currently supported patterns.

                                                                                                          Examples:

                                                                                                          Equations
                                                                                                          Instances For
                                                                                                            @[deprecated String.dropSuffix (since := "2025-11-17")]
                                                                                                            Equations
                                                                                                            Instances For
                                                                                                              @[deprecated String.Slice.dropSuffix (since := "2025-11-20")]
                                                                                                              Equations
                                                                                                              Instances For