1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Unicode string slices.
13 //! The `&str` type is one of the two main string types, the other being `String`.
14 //! Unlike its `String` counterpart, its contents are borrowed.
18 //! A basic string declaration of `&str` type:
21 //! let hello_world = "Hello, World!";
24 //! Here we have declared a string literal, also known as a string slice.
25 //! String literals have a static lifetime, which means the string `hello_world`
26 //! is guaranteed to be valid for the duration of the entire program.
27 //! We can explicitly specify `hello_world`'s lifetime as well:
30 //! let hello_world: &'static str = "Hello, world!";
33 //! *[See also the `str` primitive type](../../std/primitive.str.html).*
35 #![stable(feature = "rust1", since = "1.0.0")]
37 // Many of the usings in this module are only used in the test configuration.
38 // It's cleaner to just turn off the unused_imports warning than to fix them.
39 #![allow(unused_imports)]
42 use core::str as core_str;
43 use core::str::pattern::Pattern;
44 use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
46 use core::iter::FusedIterator;
47 use std_unicode::str::{UnicodeStr, Utf16Encoder};
49 use vec_deque::VecDeque;
50 use borrow::{Borrow, ToOwned};
54 use slice::{SliceConcatExt, SliceIndex};
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub use core::str::{FromStr, Utf8Error};
60 #[stable(feature = "rust1", since = "1.0.0")]
61 pub use core::str::{Lines, LinesAny};
62 #[stable(feature = "rust1", since = "1.0.0")]
63 pub use core::str::{Split, RSplit};
64 #[stable(feature = "rust1", since = "1.0.0")]
65 pub use core::str::{SplitN, RSplitN};
66 #[stable(feature = "rust1", since = "1.0.0")]
67 pub use core::str::{SplitTerminator, RSplitTerminator};
68 #[stable(feature = "rust1", since = "1.0.0")]
69 pub use core::str::{Matches, RMatches};
70 #[stable(feature = "rust1", since = "1.0.0")]
71 pub use core::str::{MatchIndices, RMatchIndices};
72 #[stable(feature = "rust1", since = "1.0.0")]
73 pub use core::str::{from_utf8, Chars, CharIndices, Bytes};
74 #[stable(feature = "rust1", since = "1.0.0")]
75 pub use core::str::{from_utf8_unchecked, from_utf8_unchecked_mut, ParseBoolError};
76 #[stable(feature = "rust1", since = "1.0.0")]
77 pub use std_unicode::str::SplitWhitespace;
78 #[stable(feature = "rust1", since = "1.0.0")]
79 pub use core::str::pattern;
81 #[unstable(feature = "slice_concat_ext",
82 reason = "trait should not have to exist",
84 impl<S: Borrow<str>> SliceConcatExt<str> for [S] {
87 fn concat(&self) -> String {
92 // `len` calculation may overflow but push_str will check boundaries
93 let len = self.iter().map(|s| s.borrow().len()).sum();
94 let mut result = String::with_capacity(len);
97 result.push_str(s.borrow())
103 fn join(&self, sep: &str) -> String {
105 return String::new();
110 return self.concat();
113 // this is wrong without the guarantee that `self` is non-empty
114 // `len` calculation may overflow but push_str but will check boundaries
115 let len = sep.len() * (self.len() - 1) +
116 self.iter().map(|s| s.borrow().len()).sum::<usize>();
117 let mut result = String::with_capacity(len);
118 let mut first = true;
124 result.push_str(sep);
126 result.push_str(s.borrow());
131 fn connect(&self, sep: &str) -> String {
136 /// An iterator of [`u16`] over the string encoded as UTF-16.
138 /// [`u16`]: ../../std/primitive.u16.html
140 /// This struct is created by the [`encode_utf16`] method on [`str`].
141 /// See its documentation for more.
143 /// [`encode_utf16`]: ../../std/primitive.str.html#method.encode_utf16
144 /// [`str`]: ../../std/primitive.str.html
146 #[stable(feature = "encode_utf16", since = "1.8.0")]
147 pub struct EncodeUtf16<'a> {
148 encoder: Utf16Encoder<Chars<'a>>,
151 #[stable(feature = "collection_debug", since = "1.17.0")]
152 impl<'a> fmt::Debug for EncodeUtf16<'a> {
153 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
154 f.pad("EncodeUtf16 { .. }")
158 #[stable(feature = "encode_utf16", since = "1.8.0")]
159 impl<'a> Iterator for EncodeUtf16<'a> {
163 fn next(&mut self) -> Option<u16> {
168 fn size_hint(&self) -> (usize, Option<usize>) {
169 self.encoder.size_hint()
173 #[unstable(feature = "fused", issue = "35602")]
174 impl<'a> FusedIterator for EncodeUtf16<'a> {}
176 // Return the initial codepoint accumulator for the first byte.
177 // The first byte is special, only want bottom 5 bits for width 2, 4 bits
178 // for width 3, and 3 bits for width 4
179 macro_rules! utf8_first_byte {
180 ($byte:expr, $width:expr) => (($byte & (0x7F >> $width)) as u32)
183 // return the value of $ch updated with continuation byte $byte
184 macro_rules! utf8_acc_cont_byte {
185 ($ch:expr, $byte:expr) => (($ch << 6) | ($byte & 63) as u32)
188 #[stable(feature = "rust1", since = "1.0.0")]
189 impl Borrow<str> for String {
191 fn borrow(&self) -> &str {
196 #[stable(feature = "rust1", since = "1.0.0")]
197 impl ToOwned for str {
199 fn to_owned(&self) -> String {
200 unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
204 /// Methods for string slices.
208 /// Returns the length of `self`.
210 /// This length is in bytes, not [`char`]s or graphemes. In other words,
211 /// it may not be what a human considers the length of the string.
213 /// [`char`]: primitive.char.html
220 /// let len = "foo".len();
221 /// assert_eq!(3, len);
223 /// let len = "ƒoo".len(); // fancy f!
224 /// assert_eq!(4, len);
226 #[stable(feature = "rust1", since = "1.0.0")]
228 pub fn len(&self) -> usize {
229 core_str::StrExt::len(self)
232 /// Returns `true` if `self` has a length of zero bytes.
240 /// assert!(s.is_empty());
242 /// let s = "not empty";
243 /// assert!(!s.is_empty());
246 #[stable(feature = "rust1", since = "1.0.0")]
247 pub fn is_empty(&self) -> bool {
248 core_str::StrExt::is_empty(self)
251 /// Checks that `index`-th byte lies at the start and/or end of a
252 /// UTF-8 code point sequence.
254 /// The start and end of the string (when `index == self.len()`) are
258 /// Returns `false` if `index` is greater than `self.len()`.
263 /// let s = "Löwe 老虎 Léopard";
264 /// assert!(s.is_char_boundary(0));
266 /// assert!(s.is_char_boundary(6));
267 /// assert!(s.is_char_boundary(s.len()));
269 /// // second byte of `ö`
270 /// assert!(!s.is_char_boundary(2));
272 /// // third byte of `老`
273 /// assert!(!s.is_char_boundary(8));
275 #[stable(feature = "is_char_boundary", since = "1.9.0")]
277 pub fn is_char_boundary(&self, index: usize) -> bool {
278 core_str::StrExt::is_char_boundary(self, index)
281 /// Converts a string slice to a byte slice.
288 /// let bytes = "bors".as_bytes();
289 /// assert_eq!(b"bors", bytes);
291 #[stable(feature = "rust1", since = "1.0.0")]
293 pub fn as_bytes(&self) -> &[u8] {
294 core_str::StrExt::as_bytes(self)
297 /// Converts a mutable string slice to a mutable byte slice.
298 #[unstable(feature = "str_mut_extras", issue = "41119")]
300 pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
301 core_str::StrExt::as_bytes_mut(self)
304 /// Converts a string slice to a raw pointer.
306 /// As string slices are a slice of bytes, the raw pointer points to a
307 /// [`u8`]. This pointer will be pointing to the first byte of the string
310 /// [`u8`]: primitive.u8.html
318 /// let ptr = s.as_ptr();
320 #[stable(feature = "rust1", since = "1.0.0")]
322 pub fn as_ptr(&self) -> *const u8 {
323 core_str::StrExt::as_ptr(self)
326 /// Returns a subslice of `str`.
328 /// This is the non-panicking alternative to indexing the `str`. Returns `None` whenever
329 /// equivalent indexing operation would panic.
334 /// # #![feature(str_checked_slicing)]
336 /// assert_eq!(Some("🗻"), v.get(0..4));
337 /// assert!(v.get(1..).is_none());
338 /// assert!(v.get(..8).is_none());
339 /// assert!(v.get(..42).is_none());
341 #[unstable(feature = "str_checked_slicing", issue = "39932")]
343 pub fn get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
344 core_str::StrExt::get(self, i)
347 /// Returns a mutable subslice of `str`.
349 /// This is the non-panicking alternative to indexing the `str`. Returns `None` whenever
350 /// equivalent indexing operation would panic.
355 /// # #![feature(str_checked_slicing)]
356 /// let mut v = String::from("🗻∈🌏");
357 /// assert_eq!(Some("🗻"), v.get_mut(0..4).map(|v| &*v));
358 /// assert!(v.get_mut(1..).is_none());
359 /// assert!(v.get_mut(..8).is_none());
360 /// assert!(v.get_mut(..42).is_none());
362 #[unstable(feature = "str_checked_slicing", issue = "39932")]
364 pub fn get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
365 core_str::StrExt::get_mut(self, i)
368 /// Returns a unchecked subslice of `str`.
370 /// This is the unchecked alternative to indexing the `str`.
374 /// Callers of this function are responsible that these preconditions are
377 /// * The starting index must come before the ending index;
378 /// * Indexes must be within bounds of the original slice;
379 /// * Indexes must lie on UTF-8 sequence boundaries.
381 /// Failing that, the returned string slice may reference invalid memory or
382 /// violate the invariants communicated by the `str` type.
387 /// # #![feature(str_checked_slicing)]
390 /// assert_eq!("🗻", v.get_unchecked(0..4));
391 /// assert_eq!("∈", v.get_unchecked(4..7));
392 /// assert_eq!("🌏", v.get_unchecked(7..11));
395 #[unstable(feature = "str_checked_slicing", issue = "39932")]
397 pub unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output {
398 core_str::StrExt::get_unchecked(self, i)
401 /// Returns a mutable, unchecked subslice of `str`.
403 /// This is the unchecked alternative to indexing the `str`.
407 /// Callers of this function are responsible that these preconditions are
410 /// * The starting index must come before the ending index;
411 /// * Indexes must be within bounds of the original slice;
412 /// * Indexes must lie on UTF-8 sequence boundaries.
414 /// Failing that, the returned string slice may reference invalid memory or
415 /// violate the invariants communicated by the `str` type.
420 /// # #![feature(str_checked_slicing)]
421 /// let mut v = String::from("🗻∈🌏");
423 /// assert_eq!("🗻", v.get_unchecked_mut(0..4));
424 /// assert_eq!("∈", v.get_unchecked_mut(4..7));
425 /// assert_eq!("🌏", v.get_unchecked_mut(7..11));
428 #[unstable(feature = "str_checked_slicing", issue = "39932")]
430 pub unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output {
431 core_str::StrExt::get_unchecked_mut(self, i)
434 /// Creates a string slice from another string slice, bypassing safety
437 /// This new slice goes from `begin` to `end`, including `begin` but
440 /// To get a mutable string slice instead, see the
441 /// [`slice_mut_unchecked`] method.
443 /// [`slice_mut_unchecked`]: #method.slice_mut_unchecked
447 /// Callers of this function are responsible that three preconditions are
450 /// * `begin` must come before `end`.
451 /// * `begin` and `end` must be byte positions within the string slice.
452 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
459 /// let s = "Löwe 老虎 Léopard";
462 /// assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
465 /// let s = "Hello, world!";
468 /// assert_eq!("world", s.slice_unchecked(7, 12));
471 #[stable(feature = "rust1", since = "1.0.0")]
473 pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
474 core_str::StrExt::slice_unchecked(self, begin, end)
477 /// Creates a string slice from another string slice, bypassing safety
480 /// This new slice goes from `begin` to `end`, including `begin` but
483 /// To get an immutable string slice instead, see the
484 /// [`slice_unchecked`] method.
486 /// [`slice_unchecked`]: #method.slice_unchecked
490 /// Callers of this function are responsible that three preconditions are
493 /// * `begin` must come before `end`.
494 /// * `begin` and `end` must be byte positions within the string slice.
495 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
496 #[stable(feature = "str_slice_mut", since = "1.5.0")]
498 pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
499 core_str::StrExt::slice_mut_unchecked(self, begin, end)
502 /// Divide one string slice into two at an index.
504 /// The argument, `mid`, should be a byte offset from the start of the
505 /// string. It must also be on the boundary of a UTF-8 code point.
507 /// The two slices returned go from the start of the string slice to `mid`,
508 /// and from `mid` to the end of the string slice.
510 /// To get mutable string slices instead, see the [`split_at_mut`]
513 /// [`split_at_mut`]: #method.split_at_mut
517 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
518 /// beyond the last code point of the string slice.
525 /// let s = "Per Martin-Löf";
527 /// let (first, last) = s.split_at(3);
529 /// assert_eq!("Per", first);
530 /// assert_eq!(" Martin-Löf", last);
533 #[stable(feature = "str_split_at", since = "1.4.0")]
534 pub fn split_at(&self, mid: usize) -> (&str, &str) {
535 core_str::StrExt::split_at(self, mid)
538 /// Divide one mutable string slice into two at an index.
540 /// The argument, `mid`, should be a byte offset from the start of the
541 /// string. It must also be on the boundary of a UTF-8 code point.
543 /// The two slices returned go from the start of the string slice to `mid`,
544 /// and from `mid` to the end of the string slice.
546 /// To get immutable string slices instead, see the [`split_at`] method.
548 /// [`split_at`]: #method.split_at
552 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
553 /// beyond the last code point of the string slice.
560 /// let mut s = "Per Martin-Löf".to_string();
562 /// let (first, last) = s.split_at_mut(3);
564 /// assert_eq!("Per", first);
565 /// assert_eq!(" Martin-Löf", last);
568 #[stable(feature = "str_split_at", since = "1.4.0")]
569 pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
570 core_str::StrExt::split_at_mut(self, mid)
573 /// Returns an iterator over the `char`s of a string slice.
575 /// As a string slice consists of valid UTF-8, we can iterate through a
576 /// string slice by [`char`]. This method returns such an iterator.
578 /// It's important to remember that [`char`] represents a Unicode Scalar
579 /// Value, and may not match your idea of what a 'character' is. Iteration
580 /// over grapheme clusters may be what you actually want.
582 /// [`char`]: primitive.char.html
589 /// let word = "goodbye";
591 /// let count = word.chars().count();
592 /// assert_eq!(7, count);
594 /// let mut chars = word.chars();
596 /// assert_eq!(Some('g'), chars.next());
597 /// assert_eq!(Some('o'), chars.next());
598 /// assert_eq!(Some('o'), chars.next());
599 /// assert_eq!(Some('d'), chars.next());
600 /// assert_eq!(Some('b'), chars.next());
601 /// assert_eq!(Some('y'), chars.next());
602 /// assert_eq!(Some('e'), chars.next());
604 /// assert_eq!(None, chars.next());
607 /// Remember, [`char`]s may not match your human intuition about characters:
612 /// let mut chars = y.chars();
614 /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
615 /// assert_eq!(Some('\u{0306}'), chars.next());
617 /// assert_eq!(None, chars.next());
619 #[stable(feature = "rust1", since = "1.0.0")]
621 pub fn chars(&self) -> Chars {
622 core_str::StrExt::chars(self)
624 /// Returns an iterator over the [`char`]s of a string slice, and their
627 /// As a string slice consists of valid UTF-8, we can iterate through a
628 /// string slice by [`char`]. This method returns an iterator of both
629 /// these [`char`]s, as well as their byte positions.
631 /// The iterator yields tuples. The position is first, the [`char`] is
634 /// [`char`]: primitive.char.html
641 /// let word = "goodbye";
643 /// let count = word.char_indices().count();
644 /// assert_eq!(7, count);
646 /// let mut char_indices = word.char_indices();
648 /// assert_eq!(Some((0, 'g')), char_indices.next());
649 /// assert_eq!(Some((1, 'o')), char_indices.next());
650 /// assert_eq!(Some((2, 'o')), char_indices.next());
651 /// assert_eq!(Some((3, 'd')), char_indices.next());
652 /// assert_eq!(Some((4, 'b')), char_indices.next());
653 /// assert_eq!(Some((5, 'y')), char_indices.next());
654 /// assert_eq!(Some((6, 'e')), char_indices.next());
656 /// assert_eq!(None, char_indices.next());
659 /// Remember, [`char`]s may not match your human intuition about characters:
664 /// let mut char_indices = y.char_indices();
666 /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
667 /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
669 /// assert_eq!(None, char_indices.next());
671 #[stable(feature = "rust1", since = "1.0.0")]
673 pub fn char_indices(&self) -> CharIndices {
674 core_str::StrExt::char_indices(self)
677 /// An iterator over the bytes of a string slice.
679 /// As a string slice consists of a sequence of bytes, we can iterate
680 /// through a string slice by byte. This method returns such an iterator.
687 /// let mut bytes = "bors".bytes();
689 /// assert_eq!(Some(b'b'), bytes.next());
690 /// assert_eq!(Some(b'o'), bytes.next());
691 /// assert_eq!(Some(b'r'), bytes.next());
692 /// assert_eq!(Some(b's'), bytes.next());
694 /// assert_eq!(None, bytes.next());
696 #[stable(feature = "rust1", since = "1.0.0")]
698 pub fn bytes(&self) -> Bytes {
699 core_str::StrExt::bytes(self)
702 /// Split a string slice by whitespace.
704 /// The iterator returned will return string slices that are sub-slices of
705 /// the original string slice, separated by any amount of whitespace.
707 /// 'Whitespace' is defined according to the terms of the Unicode Derived
708 /// Core Property `White_Space`.
715 /// let mut iter = "A few words".split_whitespace();
717 /// assert_eq!(Some("A"), iter.next());
718 /// assert_eq!(Some("few"), iter.next());
719 /// assert_eq!(Some("words"), iter.next());
721 /// assert_eq!(None, iter.next());
724 /// All kinds of whitespace are considered:
727 /// let mut iter = " Mary had\ta\u{2009}little \n\t lamb".split_whitespace();
728 /// assert_eq!(Some("Mary"), iter.next());
729 /// assert_eq!(Some("had"), iter.next());
730 /// assert_eq!(Some("a"), iter.next());
731 /// assert_eq!(Some("little"), iter.next());
732 /// assert_eq!(Some("lamb"), iter.next());
734 /// assert_eq!(None, iter.next());
736 #[stable(feature = "split_whitespace", since = "1.1.0")]
738 pub fn split_whitespace(&self) -> SplitWhitespace {
739 UnicodeStr::split_whitespace(self)
742 /// An iterator over the lines of a string, as string slices.
744 /// Lines are ended with either a newline (`\n`) or a carriage return with
745 /// a line feed (`\r\n`).
747 /// The final line ending is optional.
754 /// let text = "foo\r\nbar\n\nbaz\n";
755 /// let mut lines = text.lines();
757 /// assert_eq!(Some("foo"), lines.next());
758 /// assert_eq!(Some("bar"), lines.next());
759 /// assert_eq!(Some(""), lines.next());
760 /// assert_eq!(Some("baz"), lines.next());
762 /// assert_eq!(None, lines.next());
765 /// The final line ending isn't required:
768 /// let text = "foo\nbar\n\r\nbaz";
769 /// let mut lines = text.lines();
771 /// assert_eq!(Some("foo"), lines.next());
772 /// assert_eq!(Some("bar"), lines.next());
773 /// assert_eq!(Some(""), lines.next());
774 /// assert_eq!(Some("baz"), lines.next());
776 /// assert_eq!(None, lines.next());
778 #[stable(feature = "rust1", since = "1.0.0")]
780 pub fn lines(&self) -> Lines {
781 core_str::StrExt::lines(self)
784 /// An iterator over the lines of a string.
785 #[stable(feature = "rust1", since = "1.0.0")]
786 #[rustc_deprecated(since = "1.4.0", reason = "use lines() instead now")]
789 pub fn lines_any(&self) -> LinesAny {
790 core_str::StrExt::lines_any(self)
793 /// Returns an iterator of `u16` over the string encoded as UTF-16.
794 #[stable(feature = "encode_utf16", since = "1.8.0")]
795 pub fn encode_utf16(&self) -> EncodeUtf16 {
796 EncodeUtf16 { encoder: Utf16Encoder::new(self[..].chars()) }
799 /// Returns `true` if the given pattern matches a sub-slice of
800 /// this string slice.
802 /// Returns `false` if it does not.
809 /// let bananas = "bananas";
811 /// assert!(bananas.contains("nana"));
812 /// assert!(!bananas.contains("apples"));
814 #[stable(feature = "rust1", since = "1.0.0")]
815 pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
816 core_str::StrExt::contains(self, pat)
819 /// Returns `true` if the given pattern matches a prefix of this
822 /// Returns `false` if it does not.
829 /// let bananas = "bananas";
831 /// assert!(bananas.starts_with("bana"));
832 /// assert!(!bananas.starts_with("nana"));
834 #[stable(feature = "rust1", since = "1.0.0")]
835 pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
836 core_str::StrExt::starts_with(self, pat)
839 /// Returns `true` if the given pattern matches a suffix of this
842 /// Returns `false` if it does not.
849 /// let bananas = "bananas";
851 /// assert!(bananas.ends_with("anas"));
852 /// assert!(!bananas.ends_with("nana"));
854 #[stable(feature = "rust1", since = "1.0.0")]
855 pub fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
856 where P::Searcher: ReverseSearcher<'a>
858 core_str::StrExt::ends_with(self, pat)
861 /// Returns the byte index of the first character of this string slice that
862 /// matches the pattern.
864 /// Returns [`None`] if the pattern doesn't match.
866 /// The pattern can be a `&str`, [`char`], or a closure that determines if
867 /// a character matches.
869 /// [`char`]: primitive.char.html
870 /// [`None`]: option/enum.Option.html#variant.None
877 /// let s = "Löwe 老虎 Léopard";
879 /// assert_eq!(s.find('L'), Some(0));
880 /// assert_eq!(s.find('é'), Some(14));
881 /// assert_eq!(s.find("Léopard"), Some(13));
884 /// More complex patterns with closures:
887 /// let s = "Löwe 老虎 Léopard";
889 /// assert_eq!(s.find(char::is_whitespace), Some(5));
890 /// assert_eq!(s.find(char::is_lowercase), Some(1));
893 /// Not finding the pattern:
896 /// let s = "Löwe 老虎 Léopard";
897 /// let x: &[_] = &['1', '2'];
899 /// assert_eq!(s.find(x), None);
901 #[stable(feature = "rust1", since = "1.0.0")]
902 pub fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
903 core_str::StrExt::find(self, pat)
906 /// Returns the byte index of the last character of this string slice that
907 /// matches the pattern.
909 /// Returns [`None`] if the pattern doesn't match.
911 /// The pattern can be a `&str`, [`char`], or a closure that determines if
912 /// a character matches.
914 /// [`char`]: primitive.char.html
915 /// [`None`]: option/enum.Option.html#variant.None
922 /// let s = "Löwe 老虎 Léopard";
924 /// assert_eq!(s.rfind('L'), Some(13));
925 /// assert_eq!(s.rfind('é'), Some(14));
928 /// More complex patterns with closures:
931 /// let s = "Löwe 老虎 Léopard";
933 /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
934 /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
937 /// Not finding the pattern:
940 /// let s = "Löwe 老虎 Léopard";
941 /// let x: &[_] = &['1', '2'];
943 /// assert_eq!(s.rfind(x), None);
945 #[stable(feature = "rust1", since = "1.0.0")]
946 pub fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
947 where P::Searcher: ReverseSearcher<'a>
949 core_str::StrExt::rfind(self, pat)
952 /// An iterator over substrings of this string slice, separated by
953 /// characters matched by a pattern.
955 /// The pattern can be a `&str`, [`char`], or a closure that determines the
958 /// # Iterator behavior
960 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
961 /// allows a reverse search and forward/reverse search yields the same
962 /// elements. This is true for, eg, [`char`] but not for `&str`.
964 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
966 /// If the pattern allows a reverse search but its results might differ
967 /// from a forward search, the [`rsplit`] method can be used.
969 /// [`char`]: primitive.char.html
970 /// [`rsplit`]: #method.rsplit
977 /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
978 /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
980 /// let v: Vec<&str> = "".split('X').collect();
981 /// assert_eq!(v, [""]);
983 /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
984 /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
986 /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
987 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
989 /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
990 /// assert_eq!(v, ["abc", "def", "ghi"]);
992 /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
993 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
996 /// A more complex pattern, using a closure:
999 /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
1000 /// assert_eq!(v, ["abc", "def", "ghi"]);
1003 /// If a string contains multiple contiguous separators, you will end up
1004 /// with empty strings in the output:
1007 /// let x = "||||a||b|c".to_string();
1008 /// let d: Vec<_> = x.split('|').collect();
1010 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1013 /// Contiguous separators are separated by the empty string.
1016 /// let x = "(///)".to_string();
1017 /// let d: Vec<_> = x.split('/').collect();;
1019 /// assert_eq!(d, &["(", "", "", ")"]);
1022 /// Separators at the start or end of a string are neighbored
1023 /// by empty strings.
1026 /// let d: Vec<_> = "010".split("0").collect();
1027 /// assert_eq!(d, &["", "1", ""]);
1030 /// When the empty string is used as a separator, it separates
1031 /// every character in the string, along with the beginning
1032 /// and end of the string.
1035 /// let f: Vec<_> = "rust".split("").collect();
1036 /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
1039 /// Contiguous separators can lead to possibly surprising behavior
1040 /// when whitespace is used as the separator. This code is correct:
1043 /// let x = " a b c".to_string();
1044 /// let d: Vec<_> = x.split(' ').collect();
1046 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1049 /// It does _not_ give you:
1052 /// assert_eq!(d, &["a", "b", "c"]);
1055 /// Use [`split_whitespace`] for this behavior.
1057 /// [`split_whitespace`]: #method.split_whitespace
1058 #[stable(feature = "rust1", since = "1.0.0")]
1059 pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
1060 core_str::StrExt::split(self, pat)
1063 /// An iterator over substrings of the given string slice, separated by
1064 /// characters matched by a pattern and yielded in reverse order.
1066 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1069 /// [`char`]: primitive.char.html
1071 /// # Iterator behavior
1073 /// The returned iterator requires that the pattern supports a reverse
1074 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1075 /// search yields the same elements.
1077 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1079 /// For iterating from the front, the [`split`] method can be used.
1081 /// [`split`]: #method.split
1085 /// Simple patterns:
1088 /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
1089 /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
1091 /// let v: Vec<&str> = "".rsplit('X').collect();
1092 /// assert_eq!(v, [""]);
1094 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
1095 /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
1097 /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
1098 /// assert_eq!(v, ["leopard", "tiger", "lion"]);
1101 /// A more complex pattern, using a closure:
1104 /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
1105 /// assert_eq!(v, ["ghi", "def", "abc"]);
1107 #[stable(feature = "rust1", since = "1.0.0")]
1108 pub fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
1109 where P::Searcher: ReverseSearcher<'a>
1111 core_str::StrExt::rsplit(self, pat)
1114 /// An iterator over substrings of the given string slice, separated by
1115 /// characters matched by a pattern.
1117 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1120 /// Equivalent to [`split`], except that the trailing substring
1121 /// is skipped if empty.
1123 /// [`split`]: #method.split
1125 /// This method can be used for string data that is _terminated_,
1126 /// rather than _separated_ by a pattern.
1128 /// # Iterator behavior
1130 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1131 /// allows a reverse search and forward/reverse search yields the same
1132 /// elements. This is true for, eg, [`char`] but not for `&str`.
1134 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1135 /// [`char`]: primitive.char.html
1137 /// If the pattern allows a reverse search but its results might differ
1138 /// from a forward search, the [`rsplit_terminator`] method can be used.
1140 /// [`rsplit_terminator`]: #method.rsplit_terminator
1147 /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
1148 /// assert_eq!(v, ["A", "B"]);
1150 /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
1151 /// assert_eq!(v, ["A", "", "B", ""]);
1153 #[stable(feature = "rust1", since = "1.0.0")]
1154 pub fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
1155 core_str::StrExt::split_terminator(self, pat)
1158 /// An iterator over substrings of `self`, separated by characters
1159 /// matched by a pattern and yielded in reverse order.
1161 /// The pattern can be a simple `&str`, [`char`], or a closure that
1162 /// determines the split.
1163 /// Additional libraries might provide more complex patterns like
1164 /// regular expressions.
1166 /// [`char`]: primitive.char.html
1168 /// Equivalent to [`split`], except that the trailing substring is
1169 /// skipped if empty.
1171 /// [`split`]: #method.split
1173 /// This method can be used for string data that is _terminated_,
1174 /// rather than _separated_ by a pattern.
1176 /// # Iterator behavior
1178 /// The returned iterator requires that the pattern supports a
1179 /// reverse search, and it will be double ended if a forward/reverse
1180 /// search yields the same elements.
1182 /// For iterating from the front, the [`split_terminator`] method can be
1185 /// [`split_terminator`]: #method.split_terminator
1190 /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
1191 /// assert_eq!(v, ["B", "A"]);
1193 /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
1194 /// assert_eq!(v, ["", "B", "", "A"]);
1196 #[stable(feature = "rust1", since = "1.0.0")]
1197 pub fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
1198 where P::Searcher: ReverseSearcher<'a>
1200 core_str::StrExt::rsplit_terminator(self, pat)
1203 /// An iterator over substrings of the given string slice, separated by a
1204 /// pattern, restricted to returning at most `n` items.
1206 /// If `n` substrings are returned, the last substring (the `n`th substring)
1207 /// will contain the remainder of the string.
1209 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1212 /// [`char`]: primitive.char.html
1214 /// # Iterator behavior
1216 /// The returned iterator will not be double ended, because it is
1217 /// not efficient to support.
1219 /// If the pattern allows a reverse search, the [`rsplitn`] method can be
1222 /// [`rsplitn`]: #method.rsplitn
1226 /// Simple patterns:
1229 /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
1230 /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
1232 /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
1233 /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
1235 /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
1236 /// assert_eq!(v, ["abcXdef"]);
1238 /// let v: Vec<&str> = "".splitn(1, 'X').collect();
1239 /// assert_eq!(v, [""]);
1242 /// A more complex pattern, using a closure:
1245 /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
1246 /// assert_eq!(v, ["abc", "defXghi"]);
1248 #[stable(feature = "rust1", since = "1.0.0")]
1249 pub fn splitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> SplitN<'a, P> {
1250 core_str::StrExt::splitn(self, n, pat)
1253 /// An iterator over substrings of this string slice, separated by a
1254 /// pattern, starting from the end of the string, restricted to returning
1255 /// at most `n` items.
1257 /// If `n` substrings are returned, the last substring (the `n`th substring)
1258 /// will contain the remainder of the string.
1260 /// The pattern can be a `&str`, [`char`], or a closure that
1261 /// determines the split.
1263 /// [`char`]: primitive.char.html
1265 /// # Iterator behavior
1267 /// The returned iterator will not be double ended, because it is not
1268 /// efficient to support.
1270 /// For splitting from the front, the [`splitn`] method can be used.
1272 /// [`splitn`]: #method.splitn
1276 /// Simple patterns:
1279 /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
1280 /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
1282 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
1283 /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
1285 /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
1286 /// assert_eq!(v, ["leopard", "lion::tiger"]);
1289 /// A more complex pattern, using a closure:
1292 /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
1293 /// assert_eq!(v, ["ghi", "abc1def"]);
1295 #[stable(feature = "rust1", since = "1.0.0")]
1296 pub fn rsplitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> RSplitN<'a, P>
1297 where P::Searcher: ReverseSearcher<'a>
1299 core_str::StrExt::rsplitn(self, n, pat)
1302 /// An iterator over the matches of a pattern within the given string
1305 /// The pattern can be a `&str`, [`char`], or a closure that
1306 /// determines if a character matches.
1308 /// [`char`]: primitive.char.html
1310 /// # Iterator behavior
1312 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1313 /// allows a reverse search and forward/reverse search yields the same
1314 /// elements. This is true for, eg, [`char`] but not for `&str`.
1316 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1317 /// [`char`]: primitive.char.html
1319 /// If the pattern allows a reverse search but its results might differ
1320 /// from a forward search, the [`rmatches`] method can be used.
1322 /// [`rmatches`]: #method.rmatches
1329 /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
1330 /// assert_eq!(v, ["abc", "abc", "abc"]);
1332 /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
1333 /// assert_eq!(v, ["1", "2", "3"]);
1335 #[stable(feature = "str_matches", since = "1.2.0")]
1336 pub fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P> {
1337 core_str::StrExt::matches(self, pat)
1340 /// An iterator over the matches of a pattern within this string slice,
1341 /// yielded in reverse order.
1343 /// The pattern can be a `&str`, [`char`], or a closure that determines if
1344 /// a character matches.
1346 /// [`char`]: primitive.char.html
1348 /// # Iterator behavior
1350 /// The returned iterator requires that the pattern supports a reverse
1351 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1352 /// search yields the same elements.
1354 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1356 /// For iterating from the front, the [`matches`] method can be used.
1358 /// [`matches`]: #method.matches
1365 /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
1366 /// assert_eq!(v, ["abc", "abc", "abc"]);
1368 /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
1369 /// assert_eq!(v, ["3", "2", "1"]);
1371 #[stable(feature = "str_matches", since = "1.2.0")]
1372 pub fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
1373 where P::Searcher: ReverseSearcher<'a>
1375 core_str::StrExt::rmatches(self, pat)
1378 /// An iterator over the disjoint matches of a pattern within this string
1379 /// slice as well as the index that the match starts at.
1381 /// For matches of `pat` within `self` that overlap, only the indices
1382 /// corresponding to the first match are returned.
1384 /// The pattern can be a `&str`, [`char`], or a closure that determines
1385 /// if a character matches.
1387 /// [`char`]: primitive.char.html
1389 /// # Iterator behavior
1391 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1392 /// allows a reverse search and forward/reverse search yields the same
1393 /// elements. This is true for, eg, [`char`] but not for `&str`.
1395 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1397 /// If the pattern allows a reverse search but its results might differ
1398 /// from a forward search, the [`rmatch_indices`] method can be used.
1400 /// [`rmatch_indices`]: #method.rmatch_indices
1407 /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
1408 /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
1410 /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
1411 /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
1413 /// let v: Vec<_> = "ababa".match_indices("aba").collect();
1414 /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
1416 #[stable(feature = "str_match_indices", since = "1.5.0")]
1417 pub fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
1418 core_str::StrExt::match_indices(self, pat)
1421 /// An iterator over the disjoint matches of a pattern within `self`,
1422 /// yielded in reverse order along with the index of the match.
1424 /// For matches of `pat` within `self` that overlap, only the indices
1425 /// corresponding to the last match are returned.
1427 /// The pattern can be a `&str`, [`char`], or a closure that determines if a
1428 /// character matches.
1430 /// [`char`]: primitive.char.html
1432 /// # Iterator behavior
1434 /// The returned iterator requires that the pattern supports a reverse
1435 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1436 /// search yields the same elements.
1438 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1440 /// For iterating from the front, the [`match_indices`] method can be used.
1442 /// [`match_indices`]: #method.match_indices
1449 /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
1450 /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
1452 /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
1453 /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
1455 /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
1456 /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
1458 #[stable(feature = "str_match_indices", since = "1.5.0")]
1459 pub fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
1460 where P::Searcher: ReverseSearcher<'a>
1462 core_str::StrExt::rmatch_indices(self, pat)
1465 /// Returns a string slice with leading and trailing whitespace removed.
1467 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1468 /// Core Property `White_Space`.
1475 /// let s = " Hello\tworld\t";
1477 /// assert_eq!("Hello\tworld", s.trim());
1479 #[stable(feature = "rust1", since = "1.0.0")]
1480 pub fn trim(&self) -> &str {
1481 UnicodeStr::trim(self)
1484 /// Returns a string slice with leading whitespace removed.
1486 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1487 /// Core Property `White_Space`.
1489 /// # Text directionality
1491 /// A string is a sequence of bytes. 'Left' in this context means the first
1492 /// position of that byte string; for a language like Arabic or Hebrew
1493 /// which are 'right to left' rather than 'left to right', this will be
1494 /// the _right_ side, not the left.
1501 /// let s = " Hello\tworld\t";
1503 /// assert_eq!("Hello\tworld\t", s.trim_left());
1509 /// let s = " English";
1510 /// assert!(Some('E') == s.trim_left().chars().next());
1512 /// let s = " עברית";
1513 /// assert!(Some('ע') == s.trim_left().chars().next());
1515 #[stable(feature = "rust1", since = "1.0.0")]
1516 pub fn trim_left(&self) -> &str {
1517 UnicodeStr::trim_left(self)
1520 /// Returns a string slice with trailing whitespace removed.
1522 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1523 /// Core Property `White_Space`.
1525 /// # Text directionality
1527 /// A string is a sequence of bytes. 'Right' in this context means the last
1528 /// position of that byte string; for a language like Arabic or Hebrew
1529 /// which are 'right to left' rather than 'left to right', this will be
1530 /// the _left_ side, not the right.
1537 /// let s = " Hello\tworld\t";
1539 /// assert_eq!(" Hello\tworld", s.trim_right());
1545 /// let s = "English ";
1546 /// assert!(Some('h') == s.trim_right().chars().rev().next());
1548 /// let s = "עברית ";
1549 /// assert!(Some('ת') == s.trim_right().chars().rev().next());
1551 #[stable(feature = "rust1", since = "1.0.0")]
1552 pub fn trim_right(&self) -> &str {
1553 UnicodeStr::trim_right(self)
1556 /// Returns a string slice with all prefixes and suffixes that match a
1557 /// pattern repeatedly removed.
1559 /// The pattern can be a [`char`] or a closure that determines if a
1560 /// character matches.
1562 /// [`char`]: primitive.char.html
1566 /// Simple patterns:
1569 /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
1570 /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
1572 /// let x: &[_] = &['1', '2'];
1573 /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
1576 /// A more complex pattern, using a closure:
1579 /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
1581 #[stable(feature = "rust1", since = "1.0.0")]
1582 pub fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
1583 where P::Searcher: DoubleEndedSearcher<'a>
1585 core_str::StrExt::trim_matches(self, pat)
1588 /// Returns a string slice with all prefixes that match a pattern
1589 /// repeatedly removed.
1591 /// The pattern can be a `&str`, [`char`], or a closure that determines if
1592 /// a character matches.
1594 /// [`char`]: primitive.char.html
1596 /// # Text directionality
1598 /// A string is a sequence of bytes. 'Left' in this context means the first
1599 /// position of that byte string; for a language like Arabic or Hebrew
1600 /// which are 'right to left' rather than 'left to right', this will be
1601 /// the _right_ side, not the left.
1608 /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
1609 /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
1611 /// let x: &[_] = &['1', '2'];
1612 /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
1614 #[stable(feature = "rust1", since = "1.0.0")]
1615 pub fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
1616 core_str::StrExt::trim_left_matches(self, pat)
1619 /// Returns a string slice with all suffixes that match a pattern
1620 /// repeatedly removed.
1622 /// The pattern can be a `&str`, [`char`], or a closure that
1623 /// determines if a character matches.
1625 /// [`char`]: primitive.char.html
1627 /// # Text directionality
1629 /// A string is a sequence of bytes. 'Right' in this context means the last
1630 /// position of that byte string; for a language like Arabic or Hebrew
1631 /// which are 'right to left' rather than 'left to right', this will be
1632 /// the _left_ side, not the right.
1636 /// Simple patterns:
1639 /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
1640 /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
1642 /// let x: &[_] = &['1', '2'];
1643 /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
1646 /// A more complex pattern, using a closure:
1649 /// assert_eq!("1fooX".trim_left_matches(|c| c == '1' || c == 'X'), "fooX");
1651 #[stable(feature = "rust1", since = "1.0.0")]
1652 pub fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
1653 where P::Searcher: ReverseSearcher<'a>
1655 core_str::StrExt::trim_right_matches(self, pat)
1658 /// Parses this string slice into another type.
1660 /// Because `parse()` is so general, it can cause problems with type
1661 /// inference. As such, `parse()` is one of the few times you'll see
1662 /// the syntax affectionately known as the 'turbofish': `::<>`. This
1663 /// helps the inference algorithm understand specifically which type
1664 /// you're trying to parse into.
1666 /// `parse()` can parse any type that implements the [`FromStr`] trait.
1668 /// [`FromStr`]: str/trait.FromStr.html
1672 /// Will return [`Err`] if it's not possible to parse this string slice into
1673 /// the desired type.
1675 /// [`Err`]: str/trait.FromStr.html#associatedtype.Err
1682 /// let four: u32 = "4".parse().unwrap();
1684 /// assert_eq!(4, four);
1687 /// Using the 'turbofish' instead of annotating `four`:
1690 /// let four = "4".parse::<u32>();
1692 /// assert_eq!(Ok(4), four);
1695 /// Failing to parse:
1698 /// let nope = "j".parse::<u32>();
1700 /// assert!(nope.is_err());
1703 #[stable(feature = "rust1", since = "1.0.0")]
1704 pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
1705 core_str::StrExt::parse(self)
1708 /// Replaces all matches of a pattern with another string.
1710 /// `replace` creates a new [`String`], and copies the data from this string slice into it.
1711 /// While doing so, it attempts to find matches of a pattern. If it finds any, it
1712 /// replaces them with the replacement string slice.
1714 /// [`String`]: string/struct.String.html
1721 /// let s = "this is old";
1723 /// assert_eq!("this is new", s.replace("old", "new"));
1726 /// When the pattern doesn't match:
1729 /// let s = "this is old";
1730 /// assert_eq!(s, s.replace("cookie monster", "little lamb"));
1732 #[stable(feature = "rust1", since = "1.0.0")]
1733 pub fn replace<'a, P: Pattern<'a>>(&'a self, from: P, to: &str) -> String {
1734 let mut result = String::new();
1735 let mut last_end = 0;
1736 for (start, part) in self.match_indices(from) {
1737 result.push_str(unsafe { self.slice_unchecked(last_end, start) });
1738 result.push_str(to);
1739 last_end = start + part.len();
1741 result.push_str(unsafe { self.slice_unchecked(last_end, self.len()) });
1745 /// Replaces first N matches of a pattern with another string.
1747 /// `replacen` creates a new [`String`], and copies the data from this string slice into it.
1748 /// While doing so, it attempts to find matches of a pattern. If it finds any, it
1749 /// replaces them with the replacement string slice at most `N` times.
1751 /// [`String`]: string/struct.String.html
1758 /// let s = "foo foo 123 foo";
1759 /// assert_eq!("new new 123 foo", s.replacen("foo", "new", 2));
1760 /// assert_eq!("faa fao 123 foo", s.replacen('o', "a", 3));
1761 /// assert_eq!("foo foo new23 foo", s.replacen(char::is_numeric, "new", 1));
1764 /// When the pattern doesn't match:
1767 /// let s = "this is old";
1768 /// assert_eq!(s, s.replacen("cookie monster", "little lamb", 10));
1770 #[stable(feature = "str_replacen", since = "1.16.0")]
1771 pub fn replacen<'a, P: Pattern<'a>>(&'a self, pat: P, to: &str, count: usize) -> String {
1772 // Hope to reduce the times of re-allocation
1773 let mut result = String::with_capacity(32);
1774 let mut last_end = 0;
1775 for (start, part) in self.match_indices(pat).take(count) {
1776 result.push_str(unsafe { self.slice_unchecked(last_end, start) });
1777 result.push_str(to);
1778 last_end = start + part.len();
1780 result.push_str(unsafe { self.slice_unchecked(last_end, self.len()) });
1784 /// Returns the lowercase equivalent of this string slice, as a new [`String`].
1786 /// 'Lowercase' is defined according to the terms of the Unicode Derived Core Property
1789 /// Since some characters can expand into multiple characters when changing
1790 /// the case, this function returns a [`String`] instead of modifying the
1791 /// parameter in-place.
1793 /// [`String`]: string/struct.String.html
1800 /// let s = "HELLO";
1802 /// assert_eq!("hello", s.to_lowercase());
1805 /// A tricky example, with sigma:
1808 /// let sigma = "Σ";
1810 /// assert_eq!("σ", sigma.to_lowercase());
1812 /// // but at the end of a word, it's ς, not σ:
1813 /// let odysseus = "ὈΔΥΣΣΕΎΣ";
1815 /// assert_eq!("ὀδυσσεύς", odysseus.to_lowercase());
1818 /// Languages without case are not changed:
1821 /// let new_year = "农历新年";
1823 /// assert_eq!(new_year, new_year.to_lowercase());
1825 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
1826 pub fn to_lowercase(&self) -> String {
1827 let mut s = String::with_capacity(self.len());
1828 for (i, c) in self[..].char_indices() {
1830 // Σ maps to σ, except at the end of a word where it maps to ς.
1831 // This is the only conditional (contextual) but language-independent mapping
1832 // in `SpecialCasing.txt`,
1833 // so hard-code it rather than have a generic "condition" mechanism.
1834 // See https://github.com/rust-lang/rust/issues/26035
1835 map_uppercase_sigma(self, i, &mut s)
1837 s.extend(c.to_lowercase());
1842 fn map_uppercase_sigma(from: &str, i: usize, to: &mut String) {
1843 // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
1844 // for the definition of `Final_Sigma`.
1845 debug_assert!('Σ'.len_utf8() == 2);
1846 let is_word_final = case_ignoreable_then_cased(from[..i].chars().rev()) &&
1847 !case_ignoreable_then_cased(from[i + 2..].chars());
1848 to.push_str(if is_word_final { "ς" } else { "σ" });
1851 fn case_ignoreable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {
1852 use std_unicode::derived_property::{Cased, Case_Ignorable};
1853 match iter.skip_while(|&c| Case_Ignorable(c)).next() {
1854 Some(c) => Cased(c),
1860 /// Returns the uppercase equivalent of this string slice, as a new [`String`].
1862 /// 'Uppercase' is defined according to the terms of the Unicode Derived Core Property
1865 /// Since some characters can expand into multiple characters when changing
1866 /// the case, this function returns a [`String`] instead of modifying the
1867 /// parameter in-place.
1869 /// [`String`]: string/struct.String.html
1876 /// let s = "hello";
1878 /// assert_eq!("HELLO", s.to_uppercase());
1881 /// Scripts without case are not changed:
1884 /// let new_year = "农历新年";
1886 /// assert_eq!(new_year, new_year.to_uppercase());
1888 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
1889 pub fn to_uppercase(&self) -> String {
1890 let mut s = String::with_capacity(self.len());
1891 s.extend(self.chars().flat_map(|c| c.to_uppercase()));
1895 /// Escapes each char in `s` with `char::escape_debug`.
1896 #[unstable(feature = "str_escape",
1897 reason = "return type may change to be an iterator",
1899 pub fn escape_debug(&self) -> String {
1900 self.chars().flat_map(|c| c.escape_debug()).collect()
1903 /// Escapes each char in `s` with `char::escape_default`.
1904 #[unstable(feature = "str_escape",
1905 reason = "return type may change to be an iterator",
1907 pub fn escape_default(&self) -> String {
1908 self.chars().flat_map(|c| c.escape_default()).collect()
1911 /// Escapes each char in `s` with `char::escape_unicode`.
1912 #[unstable(feature = "str_escape",
1913 reason = "return type may change to be an iterator",
1915 pub fn escape_unicode(&self) -> String {
1916 self.chars().flat_map(|c| c.escape_unicode()).collect()
1919 /// Converts a `Box<str>` into a [`String`] without copying or allocating.
1921 /// [`String`]: string/struct.String.html
1928 /// let string = String::from("birthday gift");
1929 /// let boxed_str = string.clone().into_boxed_str();
1931 /// assert_eq!(boxed_str.into_string(), string);
1933 #[stable(feature = "box_str", since = "1.4.0")]
1934 pub fn into_string(self: Box<str>) -> String {
1936 let slice = mem::transmute::<Box<str>, Box<[u8]>>(self);
1937 String::from_utf8_unchecked(slice.into_vec())
1941 /// Create a [`String`] by repeating a string `n` times.
1943 /// [`String`]: string/struct.String.html
1950 /// assert_eq!("abc".repeat(4), String::from("abcabcabcabc"));
1952 #[stable(feature = "repeat_str", since = "1.16.0")]
1953 pub fn repeat(&self, n: usize) -> String {
1954 let mut s = String::with_capacity(self.len() * n);
1955 s.extend((0..n).map(|_| self));