1 // Copyright 2012-2017 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, from_utf8_mut, 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
;
82 #[unstable(feature = "slice_concat_ext",
83 reason
= "trait should not have to exist",
85 impl<S
: Borrow
<str>> SliceConcatExt
<str> for [S
] {
88 fn concat(&self) -> String
{
93 // `len` calculation may overflow but push_str will check boundaries
94 let len
= self.iter().map(|s
| s
.borrow().len()).sum();
95 let mut result
= String
::with_capacity(len
);
98 result
.push_str(s
.borrow())
104 fn join(&self, sep
: &str) -> String
{
106 return String
::new();
111 return self.concat();
114 // this is wrong without the guarantee that `self` is non-empty
115 // `len` calculation may overflow but push_str but will check boundaries
116 let len
= sep
.len() * (self.len() - 1) +
117 self.iter().map(|s
| s
.borrow().len()).sum
::<usize>();
118 let mut result
= String
::with_capacity(len
);
119 let mut first
= true;
125 result
.push_str(sep
);
127 result
.push_str(s
.borrow());
132 fn connect(&self, sep
: &str) -> String
{
137 /// An iterator of [`u16`] over the string encoded as UTF-16.
139 /// [`u16`]: ../../std/primitive.u16.html
141 /// This struct is created by the [`encode_utf16`] method on [`str`].
142 /// See its documentation for more.
144 /// [`encode_utf16`]: ../../std/primitive.str.html#method.encode_utf16
145 /// [`str`]: ../../std/primitive.str.html
147 #[stable(feature = "encode_utf16", since = "1.8.0")]
148 pub struct EncodeUtf16
<'a
> {
149 encoder
: Utf16Encoder
<Chars
<'a
>>,
152 #[stable(feature = "collection_debug", since = "1.17.0")]
153 impl<'a
> fmt
::Debug
for EncodeUtf16
<'a
> {
154 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
155 f
.pad("EncodeUtf16 { .. }")
159 #[stable(feature = "encode_utf16", since = "1.8.0")]
160 impl<'a
> Iterator
for EncodeUtf16
<'a
> {
164 fn next(&mut self) -> Option
<u16> {
169 fn size_hint(&self) -> (usize, Option
<usize>) {
170 self.encoder
.size_hint()
174 #[unstable(feature = "fused", issue = "35602")]
175 impl<'a
> FusedIterator
for EncodeUtf16
<'a
> {}
177 #[stable(feature = "rust1", since = "1.0.0")]
178 impl Borrow
<str> for String
{
180 fn borrow(&self) -> &str {
185 #[stable(feature = "rust1", since = "1.0.0")]
186 impl ToOwned
for str {
188 fn to_owned(&self) -> String
{
189 unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
192 fn clone_into(&self, target
: &mut String
) {
193 let mut b
= mem
::replace(target
, String
::new()).into_bytes();
194 self.as_bytes().clone_into(&mut b
);
195 *target
= unsafe { String::from_utf8_unchecked(b) }
199 /// Methods for string slices.
203 /// Returns the length of `self`.
205 /// This length is in bytes, not [`char`]s or graphemes. In other words,
206 /// it may not be what a human considers the length of the string.
208 /// [`char`]: primitive.char.html
215 /// let len = "foo".len();
216 /// assert_eq!(3, len);
218 /// let len = "ƒoo".len(); // fancy f!
219 /// assert_eq!(4, len);
221 #[stable(feature = "rust1", since = "1.0.0")]
223 pub fn len(&self) -> usize {
224 core_str
::StrExt
::len(self)
227 /// Returns `true` if `self` has a length of zero bytes.
235 /// assert!(s.is_empty());
237 /// let s = "not empty";
238 /// assert!(!s.is_empty());
241 #[stable(feature = "rust1", since = "1.0.0")]
242 pub fn is_empty(&self) -> bool
{
243 core_str
::StrExt
::is_empty(self)
246 /// Checks that `index`-th byte lies at the start and/or end of a
247 /// UTF-8 code point sequence.
249 /// The start and end of the string (when `index == self.len()`) are
253 /// Returns `false` if `index` is greater than `self.len()`.
258 /// let s = "Löwe 老虎 Léopard";
259 /// assert!(s.is_char_boundary(0));
261 /// assert!(s.is_char_boundary(6));
262 /// assert!(s.is_char_boundary(s.len()));
264 /// // second byte of `ö`
265 /// assert!(!s.is_char_boundary(2));
267 /// // third byte of `老`
268 /// assert!(!s.is_char_boundary(8));
270 #[stable(feature = "is_char_boundary", since = "1.9.0")]
272 pub fn is_char_boundary(&self, index
: usize) -> bool
{
273 core_str
::StrExt
::is_char_boundary(self, index
)
276 /// Converts a string slice to a byte slice.
283 /// let bytes = "bors".as_bytes();
284 /// assert_eq!(b"bors", bytes);
286 #[stable(feature = "rust1", since = "1.0.0")]
288 pub fn as_bytes(&self) -> &[u8] {
289 core_str
::StrExt
::as_bytes(self)
292 /// Converts a mutable string slice to a mutable byte slice.
293 #[stable(feature = "str_mut_extras", since = "1.20.0")]
295 pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
296 core_str
::StrExt
::as_bytes_mut(self)
299 /// Converts a string slice to a raw pointer.
301 /// As string slices are a slice of bytes, the raw pointer points to a
302 /// [`u8`]. This pointer will be pointing to the first byte of the string
305 /// [`u8`]: primitive.u8.html
313 /// let ptr = s.as_ptr();
315 #[stable(feature = "rust1", since = "1.0.0")]
317 pub fn as_ptr(&self) -> *const u8 {
318 core_str
::StrExt
::as_ptr(self)
321 /// Returns a subslice of `str`.
323 /// This is the non-panicking alternative to indexing the `str`. Returns
324 /// [`None`] whenever equivalent indexing operation would panic.
326 /// [`None`]: option/enum.Option.html#variant.None
332 /// assert_eq!(Some("🗻"), v.get(0..4));
333 /// assert!(v.get(1..).is_none());
334 /// assert!(v.get(..8).is_none());
335 /// assert!(v.get(..42).is_none());
337 #[stable(feature = "str_checked_slicing", since = "1.20.0")]
339 pub fn get
<I
: SliceIndex
<str>>(&self, i
: I
) -> Option
<&I
::Output
> {
340 core_str
::StrExt
::get(self, i
)
343 /// Returns a mutable subslice of `str`.
345 /// This is the non-panicking alternative to indexing the `str`. Returns
346 /// [`None`] whenever equivalent indexing operation would panic.
348 /// [`None`]: option/enum.Option.html#variant.None
353 /// let mut v = String::from("🗻∈🌏");
354 /// assert_eq!(Some("🗻"), v.get_mut(0..4).map(|v| &*v));
355 /// assert!(v.get_mut(1..).is_none());
356 /// assert!(v.get_mut(..8).is_none());
357 /// assert!(v.get_mut(..42).is_none());
359 #[stable(feature = "str_checked_slicing", since = "1.20.0")]
361 pub fn get_mut
<I
: SliceIndex
<str>>(&mut self, i
: I
) -> Option
<&mut I
::Output
> {
362 core_str
::StrExt
::get_mut(self, i
)
365 /// Returns a unchecked subslice of `str`.
367 /// This is the unchecked alternative to indexing the `str`.
371 /// Callers of this function are responsible that these preconditions are
374 /// * The starting index must come before the ending index;
375 /// * Indexes must be within bounds of the original slice;
376 /// * Indexes must lie on UTF-8 sequence boundaries.
378 /// Failing that, the returned string slice may reference invalid memory or
379 /// violate the invariants communicated by the `str` type.
386 /// assert_eq!("🗻", v.get_unchecked(0..4));
387 /// assert_eq!("∈", v.get_unchecked(4..7));
388 /// assert_eq!("🌏", v.get_unchecked(7..11));
391 #[stable(feature = "str_checked_slicing", since = "1.20.0")]
393 pub unsafe fn get_unchecked
<I
: SliceIndex
<str>>(&self, i
: I
) -> &I
::Output
{
394 core_str
::StrExt
::get_unchecked(self, i
)
397 /// Returns a mutable, unchecked subslice of `str`.
399 /// This is the unchecked alternative to indexing the `str`.
403 /// Callers of this function are responsible that these preconditions are
406 /// * The starting index must come before the ending index;
407 /// * Indexes must be within bounds of the original slice;
408 /// * Indexes must lie on UTF-8 sequence boundaries.
410 /// Failing that, the returned string slice may reference invalid memory or
411 /// violate the invariants communicated by the `str` type.
416 /// let mut v = String::from("🗻∈🌏");
418 /// assert_eq!("🗻", v.get_unchecked_mut(0..4));
419 /// assert_eq!("∈", v.get_unchecked_mut(4..7));
420 /// assert_eq!("🌏", v.get_unchecked_mut(7..11));
423 #[stable(feature = "str_checked_slicing", since = "1.20.0")]
425 pub unsafe fn get_unchecked_mut
<I
: SliceIndex
<str>>(&mut self, i
: I
) -> &mut I
::Output
{
426 core_str
::StrExt
::get_unchecked_mut(self, i
)
429 /// Creates a string slice from another string slice, bypassing safety
432 /// This is generally not recommended, use with caution! For a safe
433 /// alternative see [`str`] and [`Index`].
435 /// [`str`]: primitive.str.html
436 /// [`Index`]: ops/trait.Index.html
438 /// This new slice goes from `begin` to `end`, including `begin` but
441 /// To get a mutable string slice instead, see the
442 /// [`slice_mut_unchecked`] method.
444 /// [`slice_mut_unchecked`]: #method.slice_mut_unchecked
448 /// Callers of this function are responsible that three preconditions are
451 /// * `begin` must come before `end`.
452 /// * `begin` and `end` must be byte positions within the string slice.
453 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
460 /// let s = "Löwe 老虎 Léopard";
463 /// assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
466 /// let s = "Hello, world!";
469 /// assert_eq!("world", s.slice_unchecked(7, 12));
472 #[stable(feature = "rust1", since = "1.0.0")]
474 pub unsafe fn slice_unchecked(&self, begin
: usize, end
: usize) -> &str {
475 core_str
::StrExt
::slice_unchecked(self, begin
, end
)
478 /// Creates a string slice from another string slice, bypassing safety
480 /// This is generally not recommended, use with caution! For a safe
481 /// alternative see [`str`] and [`IndexMut`].
483 /// [`str`]: primitive.str.html
484 /// [`IndexMut`]: ops/trait.IndexMut.html
486 /// This new slice goes from `begin` to `end`, including `begin` but
489 /// To get an immutable string slice instead, see the
490 /// [`slice_unchecked`] method.
492 /// [`slice_unchecked`]: #method.slice_unchecked
496 /// Callers of this function are responsible that three preconditions are
499 /// * `begin` must come before `end`.
500 /// * `begin` and `end` must be byte positions within the string slice.
501 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
502 #[stable(feature = "str_slice_mut", since = "1.5.0")]
504 pub unsafe fn slice_mut_unchecked(&mut self, begin
: usize, end
: usize) -> &mut str {
505 core_str
::StrExt
::slice_mut_unchecked(self, begin
, end
)
508 /// Divide one string slice into two at an index.
510 /// The argument, `mid`, should be a byte offset from the start of the
511 /// string. It must also be on the boundary of a UTF-8 code point.
513 /// The two slices returned go from the start of the string slice to `mid`,
514 /// and from `mid` to the end of the string slice.
516 /// To get mutable string slices instead, see the [`split_at_mut`]
519 /// [`split_at_mut`]: #method.split_at_mut
523 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
524 /// beyond the last code point of the string slice.
531 /// let s = "Per Martin-Löf";
533 /// let (first, last) = s.split_at(3);
535 /// assert_eq!("Per", first);
536 /// assert_eq!(" Martin-Löf", last);
539 #[stable(feature = "str_split_at", since = "1.4.0")]
540 pub fn split_at(&self, mid
: usize) -> (&str, &str) {
541 core_str
::StrExt
::split_at(self, mid
)
544 /// Divide one mutable string slice into two at an index.
546 /// The argument, `mid`, should be a byte offset from the start of the
547 /// string. It must also be on the boundary of a UTF-8 code point.
549 /// The two slices returned go from the start of the string slice to `mid`,
550 /// and from `mid` to the end of the string slice.
552 /// To get immutable string slices instead, see the [`split_at`] method.
554 /// [`split_at`]: #method.split_at
558 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
559 /// beyond the last code point of the string slice.
566 /// let mut s = "Per Martin-Löf".to_string();
568 /// let (first, last) = s.split_at_mut(3);
570 /// assert_eq!("Per", first);
571 /// assert_eq!(" Martin-Löf", last);
574 #[stable(feature = "str_split_at", since = "1.4.0")]
575 pub fn split_at_mut(&mut self, mid
: usize) -> (&mut str, &mut str) {
576 core_str
::StrExt
::split_at_mut(self, mid
)
579 /// Returns an iterator over the [`char`]s of a string slice.
581 /// As a string slice consists of valid UTF-8, we can iterate through a
582 /// string slice by [`char`]. This method returns such an iterator.
584 /// It's important to remember that [`char`] represents a Unicode Scalar
585 /// Value, and may not match your idea of what a 'character' is. Iteration
586 /// over grapheme clusters may be what you actually want.
588 /// [`char`]: primitive.char.html
595 /// let word = "goodbye";
597 /// let count = word.chars().count();
598 /// assert_eq!(7, count);
600 /// let mut chars = word.chars();
602 /// assert_eq!(Some('g'), chars.next());
603 /// assert_eq!(Some('o'), chars.next());
604 /// assert_eq!(Some('o'), chars.next());
605 /// assert_eq!(Some('d'), chars.next());
606 /// assert_eq!(Some('b'), chars.next());
607 /// assert_eq!(Some('y'), chars.next());
608 /// assert_eq!(Some('e'), chars.next());
610 /// assert_eq!(None, chars.next());
613 /// Remember, [`char`]s may not match your human intuition about characters:
618 /// let mut chars = y.chars();
620 /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
621 /// assert_eq!(Some('\u{0306}'), chars.next());
623 /// assert_eq!(None, chars.next());
625 #[stable(feature = "rust1", since = "1.0.0")]
627 pub fn chars(&self) -> Chars
{
628 core_str
::StrExt
::chars(self)
630 /// Returns an iterator over the [`char`]s of a string slice, and their
633 /// As a string slice consists of valid UTF-8, we can iterate through a
634 /// string slice by [`char`]. This method returns an iterator of both
635 /// these [`char`]s, as well as their byte positions.
637 /// The iterator yields tuples. The position is first, the [`char`] is
640 /// [`char`]: primitive.char.html
647 /// let word = "goodbye";
649 /// let count = word.char_indices().count();
650 /// assert_eq!(7, count);
652 /// let mut char_indices = word.char_indices();
654 /// assert_eq!(Some((0, 'g')), char_indices.next());
655 /// assert_eq!(Some((1, 'o')), char_indices.next());
656 /// assert_eq!(Some((2, 'o')), char_indices.next());
657 /// assert_eq!(Some((3, 'd')), char_indices.next());
658 /// assert_eq!(Some((4, 'b')), char_indices.next());
659 /// assert_eq!(Some((5, 'y')), char_indices.next());
660 /// assert_eq!(Some((6, 'e')), char_indices.next());
662 /// assert_eq!(None, char_indices.next());
665 /// Remember, [`char`]s may not match your human intuition about characters:
670 /// let mut char_indices = y.char_indices();
672 /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
673 /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
675 /// assert_eq!(None, char_indices.next());
677 #[stable(feature = "rust1", since = "1.0.0")]
679 pub fn char_indices(&self) -> CharIndices
{
680 core_str
::StrExt
::char_indices(self)
683 /// An iterator over the bytes of a string slice.
685 /// As a string slice consists of a sequence of bytes, we can iterate
686 /// through a string slice by byte. This method returns such an iterator.
693 /// let mut bytes = "bors".bytes();
695 /// assert_eq!(Some(b'b'), bytes.next());
696 /// assert_eq!(Some(b'o'), bytes.next());
697 /// assert_eq!(Some(b'r'), bytes.next());
698 /// assert_eq!(Some(b's'), bytes.next());
700 /// assert_eq!(None, bytes.next());
702 #[stable(feature = "rust1", since = "1.0.0")]
704 pub fn bytes(&self) -> Bytes
{
705 core_str
::StrExt
::bytes(self)
708 /// Split a string slice by whitespace.
710 /// The iterator returned will return string slices that are sub-slices of
711 /// the original string slice, separated by any amount of whitespace.
713 /// 'Whitespace' is defined according to the terms of the Unicode Derived
714 /// Core Property `White_Space`.
721 /// let mut iter = "A few words".split_whitespace();
723 /// assert_eq!(Some("A"), iter.next());
724 /// assert_eq!(Some("few"), iter.next());
725 /// assert_eq!(Some("words"), iter.next());
727 /// assert_eq!(None, iter.next());
730 /// All kinds of whitespace are considered:
733 /// let mut iter = " Mary had\ta\u{2009}little \n\t lamb".split_whitespace();
734 /// assert_eq!(Some("Mary"), iter.next());
735 /// assert_eq!(Some("had"), iter.next());
736 /// assert_eq!(Some("a"), iter.next());
737 /// assert_eq!(Some("little"), iter.next());
738 /// assert_eq!(Some("lamb"), iter.next());
740 /// assert_eq!(None, iter.next());
742 #[stable(feature = "split_whitespace", since = "1.1.0")]
744 pub fn split_whitespace(&self) -> SplitWhitespace
{
745 UnicodeStr
::split_whitespace(self)
748 /// An iterator over the lines of a string, as string slices.
750 /// Lines are ended with either a newline (`\n`) or a carriage return with
751 /// a line feed (`\r\n`).
753 /// The final line ending is optional.
760 /// let text = "foo\r\nbar\n\nbaz\n";
761 /// let mut lines = text.lines();
763 /// assert_eq!(Some("foo"), lines.next());
764 /// assert_eq!(Some("bar"), lines.next());
765 /// assert_eq!(Some(""), lines.next());
766 /// assert_eq!(Some("baz"), lines.next());
768 /// assert_eq!(None, lines.next());
771 /// The final line ending isn't required:
774 /// let text = "foo\nbar\n\r\nbaz";
775 /// let mut lines = text.lines();
777 /// assert_eq!(Some("foo"), lines.next());
778 /// assert_eq!(Some("bar"), lines.next());
779 /// assert_eq!(Some(""), lines.next());
780 /// assert_eq!(Some("baz"), lines.next());
782 /// assert_eq!(None, lines.next());
784 #[stable(feature = "rust1", since = "1.0.0")]
786 pub fn lines(&self) -> Lines
{
787 core_str
::StrExt
::lines(self)
790 /// An iterator over the lines of a string.
791 #[stable(feature = "rust1", since = "1.0.0")]
792 #[rustc_deprecated(since = "1.4.0", reason = "use lines() instead now")]
795 pub fn lines_any(&self) -> LinesAny
{
796 core_str
::StrExt
::lines_any(self)
799 /// Returns an iterator of `u16` over the string encoded as UTF-16.
800 #[stable(feature = "encode_utf16", since = "1.8.0")]
801 pub fn encode_utf16(&self) -> EncodeUtf16
{
802 EncodeUtf16 { encoder: Utf16Encoder::new(self[..].chars()) }
805 /// Returns `true` if the given pattern matches a sub-slice of
806 /// this string slice.
808 /// Returns `false` if it does not.
815 /// let bananas = "bananas";
817 /// assert!(bananas.contains("nana"));
818 /// assert!(!bananas.contains("apples"));
820 #[stable(feature = "rust1", since = "1.0.0")]
822 pub fn contains
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
{
823 core_str
::StrExt
::contains(self, pat
)
826 /// Returns `true` if the given pattern matches a prefix of this
829 /// Returns `false` if it does not.
836 /// let bananas = "bananas";
838 /// assert!(bananas.starts_with("bana"));
839 /// assert!(!bananas.starts_with("nana"));
841 #[stable(feature = "rust1", since = "1.0.0")]
842 pub fn starts_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
{
843 core_str
::StrExt
::starts_with(self, pat
)
846 /// Returns `true` if the given pattern matches a suffix of this
849 /// Returns `false` if it does not.
856 /// let bananas = "bananas";
858 /// assert!(bananas.ends_with("anas"));
859 /// assert!(!bananas.ends_with("nana"));
861 #[stable(feature = "rust1", since = "1.0.0")]
862 pub fn ends_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
863 where P
::Searcher
: ReverseSearcher
<'a
>
865 core_str
::StrExt
::ends_with(self, pat
)
868 /// Returns the byte index of the first character of this string slice that
869 /// matches the pattern.
871 /// Returns [`None`] if the pattern doesn't match.
873 /// The pattern can be a `&str`, [`char`], or a closure that determines if
874 /// a character matches.
876 /// [`char`]: primitive.char.html
877 /// [`None`]: option/enum.Option.html#variant.None
884 /// let s = "Löwe 老虎 Léopard";
886 /// assert_eq!(s.find('L'), Some(0));
887 /// assert_eq!(s.find('é'), Some(14));
888 /// assert_eq!(s.find("Léopard"), Some(13));
891 /// More complex patterns with closures:
894 /// let s = "Löwe 老虎 Léopard";
896 /// assert_eq!(s.find(char::is_whitespace), Some(5));
897 /// assert_eq!(s.find(char::is_lowercase), Some(1));
900 /// Not finding the pattern:
903 /// let s = "Löwe 老虎 Léopard";
904 /// let x: &[_] = &['1', '2'];
906 /// assert_eq!(s.find(x), None);
908 #[stable(feature = "rust1", since = "1.0.0")]
910 pub fn find
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize> {
911 core_str
::StrExt
::find(self, pat
)
914 /// Returns the byte index of the last character of this string slice that
915 /// matches the pattern.
917 /// Returns [`None`] if the pattern doesn't match.
919 /// The pattern can be a `&str`, [`char`], or a closure that determines if
920 /// a character matches.
922 /// [`char`]: primitive.char.html
923 /// [`None`]: option/enum.Option.html#variant.None
930 /// let s = "Löwe 老虎 Léopard";
932 /// assert_eq!(s.rfind('L'), Some(13));
933 /// assert_eq!(s.rfind('é'), Some(14));
936 /// More complex patterns with closures:
939 /// let s = "Löwe 老虎 Léopard";
941 /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
942 /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
945 /// Not finding the pattern:
948 /// let s = "Löwe 老虎 Léopard";
949 /// let x: &[_] = &['1', '2'];
951 /// assert_eq!(s.rfind(x), None);
953 #[stable(feature = "rust1", since = "1.0.0")]
955 pub fn rfind
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>
956 where P
::Searcher
: ReverseSearcher
<'a
>
958 core_str
::StrExt
::rfind(self, pat
)
961 /// An iterator over substrings of this string slice, separated by
962 /// characters matched by a pattern.
964 /// The pattern can be a `&str`, [`char`], or a closure that determines the
967 /// # Iterator behavior
969 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
970 /// allows a reverse search and forward/reverse search yields the same
971 /// elements. This is true for, eg, [`char`] but not for `&str`.
973 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
975 /// If the pattern allows a reverse search but its results might differ
976 /// from a forward search, the [`rsplit`] method can be used.
978 /// [`char`]: primitive.char.html
979 /// [`rsplit`]: #method.rsplit
986 /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
987 /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
989 /// let v: Vec<&str> = "".split('X').collect();
990 /// assert_eq!(v, [""]);
992 /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
993 /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
995 /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
996 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
998 /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
999 /// assert_eq!(v, ["abc", "def", "ghi"]);
1001 /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
1002 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
1005 /// A more complex pattern, using a closure:
1008 /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
1009 /// assert_eq!(v, ["abc", "def", "ghi"]);
1012 /// If a string contains multiple contiguous separators, you will end up
1013 /// with empty strings in the output:
1016 /// let x = "||||a||b|c".to_string();
1017 /// let d: Vec<_> = x.split('|').collect();
1019 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1022 /// Contiguous separators are separated by the empty string.
1025 /// let x = "(///)".to_string();
1026 /// let d: Vec<_> = x.split('/').collect();
1028 /// assert_eq!(d, &["(", "", "", ")"]);
1031 /// Separators at the start or end of a string are neighbored
1032 /// by empty strings.
1035 /// let d: Vec<_> = "010".split("0").collect();
1036 /// assert_eq!(d, &["", "1", ""]);
1039 /// When the empty string is used as a separator, it separates
1040 /// every character in the string, along with the beginning
1041 /// and end of the string.
1044 /// let f: Vec<_> = "rust".split("").collect();
1045 /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
1048 /// Contiguous separators can lead to possibly surprising behavior
1049 /// when whitespace is used as the separator. This code is correct:
1052 /// let x = " a b c".to_string();
1053 /// let d: Vec<_> = x.split(' ').collect();
1055 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1058 /// It does _not_ give you:
1061 /// assert_eq!(d, &["a", "b", "c"]);
1064 /// Use [`split_whitespace`] for this behavior.
1066 /// [`split_whitespace`]: #method.split_whitespace
1067 #[stable(feature = "rust1", since = "1.0.0")]
1069 pub fn split
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Split
<'a
, P
> {
1070 core_str
::StrExt
::split(self, pat
)
1073 /// An iterator over substrings of the given string slice, separated by
1074 /// characters matched by a pattern and yielded in reverse order.
1076 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1079 /// [`char`]: primitive.char.html
1081 /// # Iterator behavior
1083 /// The returned iterator requires that the pattern supports a reverse
1084 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1085 /// search yields the same elements.
1087 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1089 /// For iterating from the front, the [`split`] method can be used.
1091 /// [`split`]: #method.split
1095 /// Simple patterns:
1098 /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
1099 /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
1101 /// let v: Vec<&str> = "".rsplit('X').collect();
1102 /// assert_eq!(v, [""]);
1104 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
1105 /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
1107 /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
1108 /// assert_eq!(v, ["leopard", "tiger", "lion"]);
1111 /// A more complex pattern, using a closure:
1114 /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
1115 /// assert_eq!(v, ["ghi", "def", "abc"]);
1117 #[stable(feature = "rust1", since = "1.0.0")]
1119 pub fn rsplit
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplit
<'a
, P
>
1120 where P
::Searcher
: ReverseSearcher
<'a
>
1122 core_str
::StrExt
::rsplit(self, pat
)
1125 /// An iterator over substrings of the given string slice, separated by
1126 /// characters matched by a pattern.
1128 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1131 /// Equivalent to [`split`], except that the trailing substring
1132 /// is skipped if empty.
1134 /// [`split`]: #method.split
1136 /// This method can be used for string data that is _terminated_,
1137 /// rather than _separated_ by a pattern.
1139 /// # Iterator behavior
1141 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1142 /// allows a reverse search and forward/reverse search yields the same
1143 /// elements. This is true for, eg, [`char`] but not for `&str`.
1145 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1146 /// [`char`]: primitive.char.html
1148 /// If the pattern allows a reverse search but its results might differ
1149 /// from a forward search, the [`rsplit_terminator`] method can be used.
1151 /// [`rsplit_terminator`]: #method.rsplit_terminator
1158 /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
1159 /// assert_eq!(v, ["A", "B"]);
1161 /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
1162 /// assert_eq!(v, ["A", "", "B", ""]);
1164 #[stable(feature = "rust1", since = "1.0.0")]
1166 pub fn split_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> SplitTerminator
<'a
, P
> {
1167 core_str
::StrExt
::split_terminator(self, pat
)
1170 /// An iterator over substrings of `self`, separated by characters
1171 /// matched by a pattern and yielded in reverse order.
1173 /// The pattern can be a simple `&str`, [`char`], or a closure that
1174 /// determines the split.
1175 /// Additional libraries might provide more complex patterns like
1176 /// regular expressions.
1178 /// [`char`]: primitive.char.html
1180 /// Equivalent to [`split`], except that the trailing substring is
1181 /// skipped if empty.
1183 /// [`split`]: #method.split
1185 /// This method can be used for string data that is _terminated_,
1186 /// rather than _separated_ by a pattern.
1188 /// # Iterator behavior
1190 /// The returned iterator requires that the pattern supports a
1191 /// reverse search, and it will be double ended if a forward/reverse
1192 /// search yields the same elements.
1194 /// For iterating from the front, the [`split_terminator`] method can be
1197 /// [`split_terminator`]: #method.split_terminator
1202 /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
1203 /// assert_eq!(v, ["B", "A"]);
1205 /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
1206 /// assert_eq!(v, ["", "B", "", "A"]);
1208 #[stable(feature = "rust1", since = "1.0.0")]
1210 pub fn rsplit_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplitTerminator
<'a
, P
>
1211 where P
::Searcher
: ReverseSearcher
<'a
>
1213 core_str
::StrExt
::rsplit_terminator(self, pat
)
1216 /// An iterator over substrings of the given string slice, separated by a
1217 /// pattern, restricted to returning at most `n` items.
1219 /// If `n` substrings are returned, the last substring (the `n`th substring)
1220 /// will contain the remainder of the string.
1222 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1225 /// [`char`]: primitive.char.html
1227 /// # Iterator behavior
1229 /// The returned iterator will not be double ended, because it is
1230 /// not efficient to support.
1232 /// If the pattern allows a reverse search, the [`rsplitn`] method can be
1235 /// [`rsplitn`]: #method.rsplitn
1239 /// Simple patterns:
1242 /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
1243 /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
1245 /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
1246 /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
1248 /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
1249 /// assert_eq!(v, ["abcXdef"]);
1251 /// let v: Vec<&str> = "".splitn(1, 'X').collect();
1252 /// assert_eq!(v, [""]);
1255 /// A more complex pattern, using a closure:
1258 /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
1259 /// assert_eq!(v, ["abc", "defXghi"]);
1261 #[stable(feature = "rust1", since = "1.0.0")]
1263 pub fn splitn
<'a
, P
: Pattern
<'a
>>(&'a
self, n
: usize, pat
: P
) -> SplitN
<'a
, P
> {
1264 core_str
::StrExt
::splitn(self, n
, pat
)
1267 /// An iterator over substrings of this string slice, separated by a
1268 /// pattern, starting from the end of the string, restricted to returning
1269 /// at most `n` items.
1271 /// If `n` substrings are returned, the last substring (the `n`th substring)
1272 /// will contain the remainder of the string.
1274 /// The pattern can be a `&str`, [`char`], or a closure that
1275 /// determines the split.
1277 /// [`char`]: primitive.char.html
1279 /// # Iterator behavior
1281 /// The returned iterator will not be double ended, because it is not
1282 /// efficient to support.
1284 /// For splitting from the front, the [`splitn`] method can be used.
1286 /// [`splitn`]: #method.splitn
1290 /// Simple patterns:
1293 /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
1294 /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
1296 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
1297 /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
1299 /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
1300 /// assert_eq!(v, ["leopard", "lion::tiger"]);
1303 /// A more complex pattern, using a closure:
1306 /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
1307 /// assert_eq!(v, ["ghi", "abc1def"]);
1309 #[stable(feature = "rust1", since = "1.0.0")]
1311 pub fn rsplitn
<'a
, P
: Pattern
<'a
>>(&'a
self, n
: usize, pat
: P
) -> RSplitN
<'a
, P
>
1312 where P
::Searcher
: ReverseSearcher
<'a
>
1314 core_str
::StrExt
::rsplitn(self, n
, pat
)
1317 /// An iterator over the disjoint matches of a pattern within the given string
1320 /// The pattern can be a `&str`, [`char`], or a closure that
1321 /// determines if a character matches.
1323 /// [`char`]: primitive.char.html
1325 /// # Iterator behavior
1327 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1328 /// allows a reverse search and forward/reverse search yields the same
1329 /// elements. This is true for, eg, [`char`] but not for `&str`.
1331 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1332 /// [`char`]: primitive.char.html
1334 /// If the pattern allows a reverse search but its results might differ
1335 /// from a forward search, the [`rmatches`] method can be used.
1337 /// [`rmatches`]: #method.rmatches
1344 /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
1345 /// assert_eq!(v, ["abc", "abc", "abc"]);
1347 /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
1348 /// assert_eq!(v, ["1", "2", "3"]);
1350 #[stable(feature = "str_matches", since = "1.2.0")]
1352 pub fn matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Matches
<'a
, P
> {
1353 core_str
::StrExt
::matches(self, pat
)
1356 /// An iterator over the disjoint matches of a pattern within this string slice,
1357 /// yielded in reverse order.
1359 /// The pattern can be a `&str`, [`char`], or a closure that determines if
1360 /// a character matches.
1362 /// [`char`]: primitive.char.html
1364 /// # Iterator behavior
1366 /// The returned iterator requires that the pattern supports a reverse
1367 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1368 /// search yields the same elements.
1370 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1372 /// For iterating from the front, the [`matches`] method can be used.
1374 /// [`matches`]: #method.matches
1381 /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
1382 /// assert_eq!(v, ["abc", "abc", "abc"]);
1384 /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
1385 /// assert_eq!(v, ["3", "2", "1"]);
1387 #[stable(feature = "str_matches", since = "1.2.0")]
1389 pub fn rmatches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatches
<'a
, P
>
1390 where P
::Searcher
: ReverseSearcher
<'a
>
1392 core_str
::StrExt
::rmatches(self, pat
)
1395 /// An iterator over the disjoint matches of a pattern within this string
1396 /// slice as well as the index that the match starts at.
1398 /// For matches of `pat` within `self` that overlap, only the indices
1399 /// corresponding to the first match are returned.
1401 /// The pattern can be a `&str`, [`char`], or a closure that determines
1402 /// if a character matches.
1404 /// [`char`]: primitive.char.html
1406 /// # Iterator behavior
1408 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1409 /// allows a reverse search and forward/reverse search yields the same
1410 /// elements. This is true for, eg, [`char`] but not for `&str`.
1412 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1414 /// If the pattern allows a reverse search but its results might differ
1415 /// from a forward search, the [`rmatch_indices`] method can be used.
1417 /// [`rmatch_indices`]: #method.rmatch_indices
1424 /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
1425 /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
1427 /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
1428 /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
1430 /// let v: Vec<_> = "ababa".match_indices("aba").collect();
1431 /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
1433 #[stable(feature = "str_match_indices", since = "1.5.0")]
1435 pub fn match_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> MatchIndices
<'a
, P
> {
1436 core_str
::StrExt
::match_indices(self, pat
)
1439 /// An iterator over the disjoint matches of a pattern within `self`,
1440 /// yielded in reverse order along with the index of the match.
1442 /// For matches of `pat` within `self` that overlap, only the indices
1443 /// corresponding to the last match are returned.
1445 /// The pattern can be a `&str`, [`char`], or a closure that determines if a
1446 /// character matches.
1448 /// [`char`]: primitive.char.html
1450 /// # Iterator behavior
1452 /// The returned iterator requires that the pattern supports a reverse
1453 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1454 /// search yields the same elements.
1456 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1458 /// For iterating from the front, the [`match_indices`] method can be used.
1460 /// [`match_indices`]: #method.match_indices
1467 /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
1468 /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
1470 /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
1471 /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
1473 /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
1474 /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
1476 #[stable(feature = "str_match_indices", since = "1.5.0")]
1478 pub fn rmatch_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatchIndices
<'a
, P
>
1479 where P
::Searcher
: ReverseSearcher
<'a
>
1481 core_str
::StrExt
::rmatch_indices(self, pat
)
1484 /// Returns a string slice with leading and trailing whitespace removed.
1486 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1487 /// Core Property `White_Space`.
1494 /// let s = " Hello\tworld\t";
1496 /// assert_eq!("Hello\tworld", s.trim());
1498 #[stable(feature = "rust1", since = "1.0.0")]
1499 pub fn trim(&self) -> &str {
1500 UnicodeStr
::trim(self)
1503 /// Returns a string slice with leading whitespace removed.
1505 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1506 /// Core Property `White_Space`.
1508 /// # Text directionality
1510 /// A string is a sequence of bytes. 'Left' in this context means the first
1511 /// position of that byte string; for a language like Arabic or Hebrew
1512 /// which are 'right to left' rather than 'left to right', this will be
1513 /// the _right_ side, not the left.
1520 /// let s = " Hello\tworld\t";
1522 /// assert_eq!("Hello\tworld\t", s.trim_left());
1528 /// let s = " English";
1529 /// assert!(Some('E') == s.trim_left().chars().next());
1531 /// let s = " עברית";
1532 /// assert!(Some('ע') == s.trim_left().chars().next());
1534 #[stable(feature = "rust1", since = "1.0.0")]
1535 pub fn trim_left(&self) -> &str {
1536 UnicodeStr
::trim_left(self)
1539 /// Returns a string slice with trailing whitespace removed.
1541 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1542 /// Core Property `White_Space`.
1544 /// # Text directionality
1546 /// A string is a sequence of bytes. 'Right' in this context means the last
1547 /// position of that byte string; for a language like Arabic or Hebrew
1548 /// which are 'right to left' rather than 'left to right', this will be
1549 /// the _left_ side, not the right.
1556 /// let s = " Hello\tworld\t";
1558 /// assert_eq!(" Hello\tworld", s.trim_right());
1564 /// let s = "English ";
1565 /// assert!(Some('h') == s.trim_right().chars().rev().next());
1567 /// let s = "עברית ";
1568 /// assert!(Some('ת') == s.trim_right().chars().rev().next());
1570 #[stable(feature = "rust1", since = "1.0.0")]
1571 pub fn trim_right(&self) -> &str {
1572 UnicodeStr
::trim_right(self)
1575 /// Returns a string slice with all prefixes and suffixes that match a
1576 /// pattern repeatedly removed.
1578 /// The pattern can be a [`char`] or a closure that determines if a
1579 /// character matches.
1581 /// [`char`]: primitive.char.html
1585 /// Simple patterns:
1588 /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
1589 /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
1591 /// let x: &[_] = &['1', '2'];
1592 /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
1595 /// A more complex pattern, using a closure:
1598 /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
1600 #[stable(feature = "rust1", since = "1.0.0")]
1601 pub fn trim_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1602 where P
::Searcher
: DoubleEndedSearcher
<'a
>
1604 core_str
::StrExt
::trim_matches(self, pat
)
1607 /// Returns a string slice with all prefixes that match a pattern
1608 /// repeatedly removed.
1610 /// The pattern can be a `&str`, [`char`], or a closure that determines if
1611 /// a character matches.
1613 /// [`char`]: primitive.char.html
1615 /// # Text directionality
1617 /// A string is a sequence of bytes. 'Left' in this context means the first
1618 /// position of that byte string; for a language like Arabic or Hebrew
1619 /// which are 'right to left' rather than 'left to right', this will be
1620 /// the _right_ side, not the left.
1627 /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
1628 /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
1630 /// let x: &[_] = &['1', '2'];
1631 /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
1633 #[stable(feature = "rust1", since = "1.0.0")]
1634 pub fn trim_left_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str {
1635 core_str
::StrExt
::trim_left_matches(self, pat
)
1638 /// Returns a string slice with all suffixes that match a pattern
1639 /// repeatedly removed.
1641 /// The pattern can be a `&str`, [`char`], or a closure that
1642 /// determines if a character matches.
1644 /// [`char`]: primitive.char.html
1646 /// # Text directionality
1648 /// A string is a sequence of bytes. 'Right' in this context means the last
1649 /// position of that byte string; for a language like Arabic or Hebrew
1650 /// which are 'right to left' rather than 'left to right', this will be
1651 /// the _left_ side, not the right.
1655 /// Simple patterns:
1658 /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
1659 /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
1661 /// let x: &[_] = &['1', '2'];
1662 /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
1665 /// A more complex pattern, using a closure:
1668 /// assert_eq!("1fooX".trim_left_matches(|c| c == '1' || c == 'X'), "fooX");
1670 #[stable(feature = "rust1", since = "1.0.0")]
1671 pub fn trim_right_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1672 where P
::Searcher
: ReverseSearcher
<'a
>
1674 core_str
::StrExt
::trim_right_matches(self, pat
)
1677 /// Parses this string slice into another type.
1679 /// Because `parse` is so general, it can cause problems with type
1680 /// inference. As such, `parse` is one of the few times you'll see
1681 /// the syntax affectionately known as the 'turbofish': `::<>`. This
1682 /// helps the inference algorithm understand specifically which type
1683 /// you're trying to parse into.
1685 /// `parse` can parse any type that implements the [`FromStr`] trait.
1687 /// [`FromStr`]: str/trait.FromStr.html
1691 /// Will return [`Err`] if it's not possible to parse this string slice into
1692 /// the desired type.
1694 /// [`Err`]: str/trait.FromStr.html#associatedtype.Err
1701 /// let four: u32 = "4".parse().unwrap();
1703 /// assert_eq!(4, four);
1706 /// Using the 'turbofish' instead of annotating `four`:
1709 /// let four = "4".parse::<u32>();
1711 /// assert_eq!(Ok(4), four);
1714 /// Failing to parse:
1717 /// let nope = "j".parse::<u32>();
1719 /// assert!(nope.is_err());
1722 #[stable(feature = "rust1", since = "1.0.0")]
1723 pub fn parse
<F
: FromStr
>(&self) -> Result
<F
, F
::Err
> {
1724 core_str
::StrExt
::parse(self)
1727 /// Converts a `Box<str>` into a `Box<[u8]>` without copying or allocating.
1728 #[stable(feature = "str_box_extras", since = "1.20.0")]
1729 pub fn into_boxed_bytes(self: Box
<str>) -> Box
<[u8]> {
1733 /// Replaces all matches of a pattern with another string.
1735 /// `replace` creates a new [`String`], and copies the data from this string slice into it.
1736 /// While doing so, it attempts to find matches of a pattern. If it finds any, it
1737 /// replaces them with the replacement string slice.
1739 /// [`String`]: string/struct.String.html
1746 /// let s = "this is old";
1748 /// assert_eq!("this is new", s.replace("old", "new"));
1751 /// When the pattern doesn't match:
1754 /// let s = "this is old";
1755 /// assert_eq!(s, s.replace("cookie monster", "little lamb"));
1757 #[stable(feature = "rust1", since = "1.0.0")]
1759 pub fn replace
<'a
, P
: Pattern
<'a
>>(&'a
self, from
: P
, to
: &str) -> String
{
1760 let mut result
= String
::new();
1761 let mut last_end
= 0;
1762 for (start
, part
) in self.match_indices(from
) {
1763 result
.push_str(unsafe { self.slice_unchecked(last_end, start) }
);
1764 result
.push_str(to
);
1765 last_end
= start
+ part
.len();
1767 result
.push_str(unsafe { self.slice_unchecked(last_end, self.len()) }
);
1771 /// Replaces first N matches of a pattern with another string.
1773 /// `replacen` creates a new [`String`], and copies the data from this string slice into it.
1774 /// While doing so, it attempts to find matches of a pattern. If it finds any, it
1775 /// replaces them with the replacement string slice at most `count` times.
1777 /// [`String`]: string/struct.String.html
1784 /// let s = "foo foo 123 foo";
1785 /// assert_eq!("new new 123 foo", s.replacen("foo", "new", 2));
1786 /// assert_eq!("faa fao 123 foo", s.replacen('o', "a", 3));
1787 /// assert_eq!("foo foo new23 foo", s.replacen(char::is_numeric, "new", 1));
1790 /// When the pattern doesn't match:
1793 /// let s = "this is old";
1794 /// assert_eq!(s, s.replacen("cookie monster", "little lamb", 10));
1796 #[stable(feature = "str_replacen", since = "1.16.0")]
1797 pub fn replacen
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
, to
: &str, count
: usize) -> String
{
1798 // Hope to reduce the times of re-allocation
1799 let mut result
= String
::with_capacity(32);
1800 let mut last_end
= 0;
1801 for (start
, part
) in self.match_indices(pat
).take(count
) {
1802 result
.push_str(unsafe { self.slice_unchecked(last_end, start) }
);
1803 result
.push_str(to
);
1804 last_end
= start
+ part
.len();
1806 result
.push_str(unsafe { self.slice_unchecked(last_end, self.len()) }
);
1810 /// Returns the lowercase equivalent of this string slice, as a new [`String`].
1812 /// 'Lowercase' is defined according to the terms of the Unicode Derived Core Property
1815 /// Since some characters can expand into multiple characters when changing
1816 /// the case, this function returns a [`String`] instead of modifying the
1817 /// parameter in-place.
1819 /// [`String`]: string/struct.String.html
1826 /// let s = "HELLO";
1828 /// assert_eq!("hello", s.to_lowercase());
1831 /// A tricky example, with sigma:
1834 /// let sigma = "Σ";
1836 /// assert_eq!("σ", sigma.to_lowercase());
1838 /// // but at the end of a word, it's ς, not σ:
1839 /// let odysseus = "ὈΔΥΣΣΕΎΣ";
1841 /// assert_eq!("ὀδυσσεύς", odysseus.to_lowercase());
1844 /// Languages without case are not changed:
1847 /// let new_year = "农历新年";
1849 /// assert_eq!(new_year, new_year.to_lowercase());
1851 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
1852 pub fn to_lowercase(&self) -> String
{
1853 let mut s
= String
::with_capacity(self.len());
1854 for (i
, c
) in self[..].char_indices() {
1856 // Σ maps to σ, except at the end of a word where it maps to ς.
1857 // This is the only conditional (contextual) but language-independent mapping
1858 // in `SpecialCasing.txt`,
1859 // so hard-code it rather than have a generic "condition" mechanism.
1860 // See https://github.com/rust-lang/rust/issues/26035
1861 map_uppercase_sigma(self, i
, &mut s
)
1863 s
.extend(c
.to_lowercase());
1868 fn map_uppercase_sigma(from
: &str, i
: usize, to
: &mut String
) {
1869 // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
1870 // for the definition of `Final_Sigma`.
1871 debug_assert
!('Σ'
.len_utf8() == 2);
1872 let is_word_final
= case_ignoreable_then_cased(from
[..i
].chars().rev()) &&
1873 !case_ignoreable_then_cased(from
[i
+ 2..].chars());
1874 to
.push_str(if is_word_final { "ς" }
else { "σ" }
);
1877 fn case_ignoreable_then_cased
<I
: Iterator
<Item
= char>>(iter
: I
) -> bool
{
1878 use std_unicode
::derived_property
::{Cased, Case_Ignorable}
;
1879 match iter
.skip_while(|&c
| Case_Ignorable(c
)).next() {
1880 Some(c
) => Cased(c
),
1886 /// Returns the uppercase equivalent of this string slice, as a new [`String`].
1888 /// 'Uppercase' is defined according to the terms of the Unicode Derived Core Property
1891 /// Since some characters can expand into multiple characters when changing
1892 /// the case, this function returns a [`String`] instead of modifying the
1893 /// parameter in-place.
1895 /// [`String`]: string/struct.String.html
1902 /// let s = "hello";
1904 /// assert_eq!("HELLO", s.to_uppercase());
1907 /// Scripts without case are not changed:
1910 /// let new_year = "农历新年";
1912 /// assert_eq!(new_year, new_year.to_uppercase());
1914 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
1915 pub fn to_uppercase(&self) -> String
{
1916 let mut s
= String
::with_capacity(self.len());
1917 s
.extend(self.chars().flat_map(|c
| c
.to_uppercase()));
1921 /// Escapes each char in `s` with [`char::escape_debug`].
1923 /// [`char::escape_debug`]: primitive.char.html#method.escape_debug
1924 #[unstable(feature = "str_escape",
1925 reason
= "return type may change to be an iterator",
1927 pub fn escape_debug(&self) -> String
{
1928 self.chars().flat_map(|c
| c
.escape_debug()).collect()
1931 /// Escapes each char in `s` with [`char::escape_default`].
1933 /// [`char::escape_default`]: primitive.char.html#method.escape_default
1934 #[unstable(feature = "str_escape",
1935 reason
= "return type may change to be an iterator",
1937 pub fn escape_default(&self) -> String
{
1938 self.chars().flat_map(|c
| c
.escape_default()).collect()
1941 /// Escapes each char in `s` with [`char::escape_unicode`].
1943 /// [`char::escape_unicode`]: primitive.char.html#method.escape_unicode
1944 #[unstable(feature = "str_escape",
1945 reason
= "return type may change to be an iterator",
1947 pub fn escape_unicode(&self) -> String
{
1948 self.chars().flat_map(|c
| c
.escape_unicode()).collect()
1951 /// Converts a [`Box<str>`] into a [`String`] without copying or allocating.
1953 /// [`String`]: string/struct.String.html
1954 /// [`Box<str>`]: boxed/struct.Box.html
1961 /// let string = String::from("birthday gift");
1962 /// let boxed_str = string.clone().into_boxed_str();
1964 /// assert_eq!(boxed_str.into_string(), string);
1966 #[stable(feature = "box_str", since = "1.4.0")]
1967 pub fn into_string(self: Box
<str>) -> String
{
1969 let slice
= mem
::transmute
::<Box
<str>, Box
<[u8]>>(self);
1970 String
::from_utf8_unchecked(slice
.into_vec())
1974 /// Create a [`String`] by repeating a string `n` times.
1976 /// [`String`]: string/struct.String.html
1983 /// assert_eq!("abc".repeat(4), String::from("abcabcabcabc"));
1985 #[stable(feature = "repeat_str", since = "1.16.0")]
1986 pub fn repeat(&self, n
: usize) -> String
{
1987 let mut s
= String
::with_capacity(self.len() * n
);
1988 s
.extend((0..n
).map(|_
| self));
1993 /// Converts a boxed slice of bytes to a boxed string slice without checking
1994 /// that the string contains valid UTF-8.
1995 #[stable(feature = "str_box_extras", since = "1.20.0")]
1996 pub unsafe fn from_boxed_utf8_unchecked(v
: Box
<[u8]>) -> Box
<str> {