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

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

                      • "red green blue".takeWhile 'r' == "r".toSlice

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

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

                      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]

                                      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]

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