字符串 String
impl String
{
#创建::
fn new() -> String
创建 usize尺寸缓冲区
fn with_capacity(capacity: usize) -> String
#1 Vec<u8> 创建 String
=> ???
fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error>
#2 Vec<u8> 创建 String
=> UTF-8 无效字符 替代 \u{FFFD}
fn from_utf8_lossy(v: &'a [u8]) -> Cow<'a, str>
#3 Vec<u8> 创建 String
=> 不检查UTF-8有效
unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String
#1 UTF-16 创建 String
=> UTF-16 无效字符 返回 None
fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error>
#2 UTF-16 创建 String
=> UTF-16 无效字符 替代 \u{FFFD}
fn from_utf16_lossy(v: &[u16]) -> String
从 [*mut 指针 , 长度 , 容量] 创建 String
unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> String
String 创建 Vec<u8>
fn into_bytes(self) -> Vec<u8>
#获取::
返回字符串的长度
fn len(&self) -> usize
判断空串 , 空串 返回 true
fn is_empty(&self) -> bool
缓冲区容量
fn capacity(&self) -> usize
返回缓冲区切片
fn as_bytes(&self) -> &[u8]
分享一个可变的字符串缓冲区的UTF-8序列
=> 不检查UTF-8有效
unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8>
转换成 Box<str>
fn into_boxed_str(self) -> Box<str>
#设置::
设置保留最小的缓冲区容量,可以避免频繁分配
fn reserve(&mut self, additional: usize)
设置缓冲区容量 additional , 可以避免频繁分配 , 满足插入预期
=> 如果缓冲区容量足够 , 不执行任何操作
fn reserve_exact(&mut self, additional: usize)
缩短缓冲区容量到字符串长度
fn shrink_to_fit(&mut self)
缩短字符串到指定的长度
fn truncate(&mut self, new_len: usize)
#拼接::
fn push(&mut self, ch: char)
fn push_str(&mut self, string: &str)
插入指定位置字符
fn insert(&mut self, idx: usize, ch: char)
#移除::
移除最后一个字符并返回他
=> 为空返回 None
fn pop(&mut self) -> Option<char>
移除指定位置字符并返回他
fn remove(&mut self, idx: usize) -> char
清除所有字符
fn clear(&mut self)
#不稳定的方法::
String 获取 &str
fn as_str(&self) -> &str
fn drain<R>(&mut self, range: R) -> Drain
where R: RangeArgument<usize>
}
&str
&str
{
得到 长度
fn len(&self) -> usize
判断空 , 空串 返回 ture
fn is_empty(&self) -> bool
得到 切片
fn as_bytes(&self) -> &[u8]
得到 裸指针
fn as_ptr(&self) -> *const u8
截取子串 [begin ... end]
=> 注意边界
unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str
在 mid之前的位置分割字符串
fn split_at(&self, mid: usize) -> (&str, &str)
在 mid之前的位置分割字符串 , 得到的是可变的引用
fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str)
创建 Chars 类型
=> 得到字符数组 ; Vec<char>
fn chars(&self) -> Chars
创建 CharIndices 类型
=> 得到 (索引,字符) 数组 ; Vec<(usize, char)>
fn char_indices(&self) -> CharIndices
创建 Bytes 类型
=> 得到字符数组 ; Vec<u8>
fn bytes(&self) -> Bytes
创建 SplitWhitespace 类型
=> 以 {空白 ' '} {对齐 '\t'} {'\u{0020}' or '\u{2009}'} 切分的字符串数组 ; Vec<&str>
fn split_whitespace(&self) -> SplitWhitespace
创建 Lines 类型
=> 以 {换行 '\n'} {回车换行 '\r''\n'} 切分的字符串数组 ; Vec<&str>
fn lines(&self) -> Lines
判断前缀是否为 参数pat , 正确返回true
fn starts_with<'a, P>(&'a self, pat: P) -> bool
where P: Pattern<'a>
判断后缀是否为 参数pat , 正确返回true
fn ends_with<'a, P>(&'a self, pat: P) -> bool
where P: Pattern<'a>, P::Searcher: ReverseSearcher<'a>
查找第一次出现的 参数pat 并返回他的位置
=> 找不到返回 None
fn find<'a, P>(&'a self, pat: P) -> Option<usize>
where P: Pattern<'a>
查找最后一次出现的 参数pat 并返回他的位置
fn rfind<'a, P>(&'a self, pat: P) -> Option<usize>
where P: Pattern<'a>, P::Searcher: ReverseSearcher<'a>
以 参数pat 切分成一个数组
fn split<'a, P>(&'a self, pat: P) -> Split<'a, P>
where P: Pattern<'a>
以 参数pat 切分成一个数组 , 并反转
fn rsplit<'a, P>(&'a self, pat: P) -> RSplit<'a, P>
where P: Pattern<'a>, P::Searcher: ReverseSearcher<'a>
以 参数pat 切分成一个数组 , ...
=> 拒绝 参数pat 为结尾的字符生成一个空串
fn split_terminator<'a, P>(&'a self, pat: P) -> SplitTerminator<'a, P>
where P: Pattern<'a>
以 参数pat 切分成一个数组 , 并反转 , ...
=> 拒绝 参数pat 为结尾的字符生成一个空串
fn rsplit_terminator<'a, P>(&'a self, pat: P) -> RSplitTerminator<'a, P>
where P: Pattern<'a>, P::Searcher: ReverseSearcher<'a>
以 参数pat 切分成一个数组 , 限制数组长度为 参数count
fn splitn<'a, P>(&'a self, count: usize, pat: P) -> SplitN<'a, P> where P: Pattern<'a>
以 参数pat 切分成一个数组 , 限制数组长度为 参数count , 并反转
fn rsplitn<'a, P>(&'a self, count: usize, pat: P) -> RSplitN<'a, P>
where P: Pattern<'a>, P::Searcher: ReverseSearcher<'a>
匹配一个包含 参数pat 的数组
fn matches<'a, P>(&'a self, pat: P) -> Matches<'a, P>
where P: Pattern<'a>
反向 匹配一个包含 参数pat 的数组
fn rmatches<'a, P>(&'a self, pat: P) -> RMatches<'a, P>
where P: Pattern<'a>, P::Searcher: ReverseSearcher<'a>
移除首尾 {空白 ' '} {对齐 '\t'} {'\u{0020}' or '\u{2009}'}
fn trim(&self) -> &str
移除首 {空白 ' '} {对齐 '\t'} {'\u{0020}' or '\u{2009}'}
fn trim_left(&self) -> &str
移除尾 {空白 ' '} {对齐 '\t'} {'\u{0020}' or '\u{2009}'}
fn trim_right(&self) -> &str
移除首尾 参数pat 指定的字符
fn trim_matches<'a, P>(&'a self, pat: P) -> &'a str
where P: Pattern<'a>, P::Searcher: DoubleEndedSearcher<'a>
移除首 参数pat 指定的字符
fn trim_left_matches<'a, P>(&'a self, pat: P) -> &'a str
where P: Pattern<'a>
移除尾 参数pat 指定的字符
fn trim_right_matches<'a, P>(&'a self, pat: P) -> &'a str
where P: Pattern<'a>, P::Searcher: ReverseSearcher<'a>
解析成指定的类型
fn parse<F>(&self) -> Result<F, F::Err>
where F: FromStr
将字符串中出现的 参数from 替换成 参数to
fn replace(&self, from: &str, to: &str) -> String
大写转小写
fn to_lowercase(&self) -> String
小写转大写
fn to_uppercase(&self) -> String
}