Documentation

Init.Data.String.TakeDrop

String.take and variants #

This file contains the implementations of String.take and its variants. Currently, they are implemented in terms of Substring; soon, they will be implemented in terms of String.Slice instead.

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

Removes the specified number of characters (Unicode code points) from the start of the string.

If n is greater than s.length, returns "".

Examples:

  • "red green blue".drop 4 = "green blue"
  • "red green blue".drop 10 = "blue"
  • "red green blue".drop 50 = ""
Equations
Instances For
    @[export lean_string_drop]
    Equations
    Instances For
      @[inline]

      Removes the specified number of characters (Unicode code points) from the end of the string.

      If n is greater than s.length, returns "".

      Examples:

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

          Creates a new string that contains the first n characters (Unicode code points) of s.

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

          Examples:

          • "red green blue".take 3 = "red"
          • "red green blue".take 1 = "r"
          • "red green blue".take 0 = ""
          • "red green blue".take 100 = "red green blue"
          Equations
          Instances For
            @[inline]

            Creates a new string that contains the last n characters (Unicode code points) of s.

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

            Examples:

            Equations
            Instances For
              @[inline]
              def String.takeWhile (s : String) (p : CharBool) :

              Creates a new string that contains the longest prefix of s in which p returns true for all characters.

              Examples:

              • "red green blue".takeWhile (·.isLetter) = "red"
              • "red green blue".takeWhile (· == 'r') = "r"
              • "red green blue".takeWhile (· != 'n') = "red gree"
              • "red green blue".takeWhile (fun _ => true) = "red green blue"
              Equations
              Instances For
                @[inline]
                def String.dropWhile (s : String) (p : CharBool) :

                Creates a new string by removing the longest prefix from s in which p returns true for all characters.

                Examples:

                • "red green blue".dropWhile (·.isLetter) = " green blue"
                • "red green blue".dropWhile (· == 'r') = "ed green blue"
                • "red green blue".dropWhile (· != 'n') = "n blue"
                • "red green blue".dropWhile (fun _ => true) = ""
                Equations
                Instances For
                  @[inline]

                  Creates a new string that contains the longest suffix of s in which p returns true for all characters.

                  Examples:

                  Equations
                  Instances For
                    @[inline]

                    Creates a new string by removing the longest suffix from s in which p returns true for all characters.

                    Examples:

                    Equations
                    Instances For
                      @[inline]

                      Checks whether the first string (s) begins with the second (pre).

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

                      Examples:

                      Equations
                      Instances For
                        @[inline]
                        def String.endsWith (s post : String) :

                        Checks whether the first string (s) ends with the second (post).

                        Examples:

                        Equations
                        Instances For
                          @[inline]

                          Removes trailing whitespace from a string.

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

                          Examples:

                          Equations
                          Instances For
                            @[inline]

                            Removes leading whitespace from a string.

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

                            Examples:

                            Equations
                            Instances For
                              @[inline]

                              Removes leading and trailing whitespace from a string.

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

                              Examples:

                              • "abc".trim = "abc"
                              • " abc".trim = "abc"
                              • "abc \t ".trim = "abc"
                              • " abc ".trim = "abc"
                              • "abc\ndef\n".trim = "abc\ndef"
                              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.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.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 pre is a prefix of s, returns the remainder. Returns none otherwise.

                                            The string pre is a prefix of s if there exists a t : String such that s = pre ++ t. If so, the result is some t.

                                            Use String.stripPrefix to return the string unchanged when pre is not a prefix.

                                            Examples:

                                            Equations
                                            Instances For

                                              If suff is a suffix of s, returns the remainder. Returns none otherwise.

                                              The string suff is a suffix of s if there exists a t : String such that s = t ++ suff. If so, the result is some t.

                                              Use String.stripSuffix to return the string unchanged when suff is not a suffix.

                                              Examples:

                                              Equations
                                              Instances For

                                                If pre is a prefix of s, returns the remainder. Returns s unmodified otherwise.

                                                The string pre is a prefix of s if there exists a t : String such that s = pre ++ t. If so, the result is t. Otherwise, it is s.

                                                Use String.dropPrefix? to return none when pre is not a prefix.

                                                Examples:

                                                Equations
                                                Instances For

                                                  If suff is a suffix of s, returns the remainder. Returns s unmodified otherwise.

                                                  The string suff is a suffix of s if there exists a t : String such that s = t ++ suff. If so, the result is t. Otherwise, it is s.

                                                  Use String.dropSuffix? to return none when suff is not a suffix.

                                                  Examples:

                                                  Equations
                                                  Instances For