Rust基础(字符串)

字符串

rust String类似于C++中的std::string,本质上是一种容纳字符串的容器。但是多了更多的操作方法。字符串字面量是Rust中的常规字符串,它本身也有一些方法。

字符串切片

字符串字面量常见的方法:

  • len:获取字符串元素的个数。
  • 常见API:
  • len(&self) -> usize:返回字符串的长度。
  • is_empty(&self) -> bool:判断字符串是否为空。
  • is_char_boundary(&self, index: usize) -> bool:检查字符串在index位置的值是否超过了字符串边界(字符串长度)。
  • floor_char_boundary(&self, index: usize) -> usize:找到最接近index但是又没有越界的索引(实验性)。
  • ceil_char_boundary(&self, index: usize) -> usize:找到最接近index但是又没有越界的索引的下一个索引(实验性)。
  • as_bytes(&self) -> &[u8]:转换字符串切片为字节切片(bytes)反向转换使用from_utf8()。类似于python中的str.encode(‘utf-8’)。
  • as_bytes_mut(&mut self) -> &mut [u8]:转换字符串切片为字节切片(bytes),转换之后的值可以被修改(unsafe)。
  • as_ptr(&self) -> *const u8:转换字符串切片为指向bytes的指针。
  • as_mut_ptr(&mut self) -> *mut u8:转换可修改字符串为指向字符串的指针(同样可修改)。
  • get<I>(&self, i: I) -> Option<&<I as SliceIndex<str>>::Output> where I: SliceIndex<str>,:返回字符串的子串。输入I的类型可以是切片类型,例如:”0..4”表示索引[0-4)。
  • get<I>(&mut self, i: I) -> Option<&<I as SliceIndex<str>>::Output> where I: SliceIndex<str>,:返回可修改的字符串的子串。输入I的类型可以是切片类型,例如:”0..4”表示索引[0-4)。
  • get_unchecked<I>(&self, i: I) -> &<I as SliceIndex<str>>::Output:返回未经检查的输入下的子串。调用者保证范围可用。(unsafe)
  • get_unchecked_mut<I>(&mut self,i: I) -> &mut <I as SliceIndex<str>>::Output where I: SliceIndex<str>:返回一个未经检查的可修改的子串。(unsafe)
  • split_at(&self, mid: usize) -> (&str, &str):将字符串使用mid作为切分位置切分为两个字符串。
  • split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str):将字符串使用mid作为切分位置切分为两个字符串,切分之后的字符串均可以被修改。
  • chars(&self) -> Chars<'_>:返回一个指向char的迭代器,char为合法utf-8。
  • char_indices(&self) -> CharIndices<'_>:返回一个字符串切片char之上的迭代器,包含有合法utf8字符的索引。
  • bytes(&self) -> Bytes<'_>:返回一个bytes类型的迭代器。
  • split_whitespace(&self) -> SplitWhitespace<'_>:返回一个使用空格分割之后的指向字符串剩余字符的迭代器。分割空格是utf-8定义的空格。
  • split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_>:返回一个使用空格分割之后的指向字符串剩余字符的迭代器。分割空格是ascii定义的空格。
  • lines(&self) -> Lines<'_>:返回一个使用换行分割之后的指向字符串剩余字符的迭代器。分割空格是ascii定义的空格\r\n\n均视为标记行的符号。
  • encode_utf16(&self) -> EncodeUtf16<'_>:返回指向utf16的u16的迭代器。
  • contains<'a, P>(&'a self, pat: P) -> bool where P: Pattern<'a>:返回字符串切片是否包含有另一个字符串。
  • starts_with<'a, P>(&'a self, pat: P) -> bool where P: Pattern<'a>:返回字符串是否已特定字符串开头。
  • ends_with<'a, P>(&'a self, pat: P) -> bool where P:Pattern<'a>:返回字符串是否以特定字符串结尾。
  • find<'a, P>(&'a self, pat: P) -> Option<usize>where P: Pattern<'a>,:在字符串中查找另一个字符P,找到则返回Some包装之后的索引,否则返回None。
  • rfind<'a, P>(&'a self, pat: P) -> Option<usize>:在字符串中反向查找另一个字符P,找到则返回Some包装之后的索引否则返回None。
  • split<'a, P>(&'a self, pat: P) -> Split<'a, P> where P: Pattern<'a>:使用一个字符P,切分字符串中的元素为字符串,返回指向字符串的迭代器,可以理解为一旦出现有P中的字符就split。
  • split_inclusive<'a, P>(&'a self, pat: P) -> SplitInclusive<'a, P> where P: Pattern<'a>:递归使用字符P切分字符串,切分的时候包含字符串。
  • rsplit<'a, P>(&'a self, pat: P) -> RSplit<'a, P> where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>:返回分割字符串之后反向迭代器。
  • split_terminator<'a, P>(&'a self, pat: P) -> SplitTerminator<'a, P> where P: Pattern<'a>:和split功能类似,除了结尾字符串为空的时候跳过。
  • rsplit_terminator<'a, P>(&'a self, pat: P) -> RSplitTerminator<'a, P> where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>:和split_terminator类似,迭代器指向反向。
  • splitn<'a, P>(&'a self, n: usize, pat: P) -> SplitN<'a, P> where P: Pattern<'a>:使用P分割字符串的时候值分割n次,返回指向对应字符串的迭代器。
  • rsplitn<'a, P>(&'a self, n: usize, pat: P) -> RSplitN<'a, P> where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>:和splitn类似,返回指向反向的迭代器。
  • split_once<'a, P>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)>:split一次,将split的两边的结果填入Option中。
  • rsplit_once<'a, P>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)> where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>:和split_once一样,填入值的方向相反。
  • matches<'a, P>(&'a self, pat: P) -> Matches<'a, P> where P: Pattern<'a>:在字符串中匹配特定的字符串,返回指向匹配字符串的迭代器。
  • rmatches<'a, P>(&'a self, pat: P) -> RMatches<'a, P> where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>:和matchs类似,迭代器指向方向相反。
  • match_indices<'a, P>(&'a self, pat: P) -> MatchIndices<'a, P> where P: Pattern<'a>:返回指向匹配字符串和字符串索引的迭代器。
  • rmatch_indices<'a, P>(&'a self, pat: P) -> RMatchIndices<'a, P> where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>:match_indices相同,迭代器指向方向相反。
  • trim(&self) -> &str:返回删除特定末尾字符串之后的字符串。
  • trim_start(&self) -> &str:从头开始返回删除特定空白字符之后的字符串。
  • trim_end(&self) -> &str:从结尾开始,返回删除特定空白符之后的字符串。
  • trim_matches<'a, P>(&'a self, pat: P) -> &'a str where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: DoubleEndedSearcher<'a>:返回删除以p开头和结尾的字符之后的字符串。
  • trim_start_matches<'a, P>(&'a self, pat: P) -> &'a str where P: Pattern<'a>:返回从头开始删除特定p之后的字符串。
  • strip_prefix<'a, P>(&'a self, prefix: P) -> Option<&'a str> where P: Pattern<'a>,:返回prefix开头的prefix移除之后的字符串。
  • strip_suffix<'a, P>(&'a self, suffix: P) -> Option<&'a str> where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>:返回suffix被删除之后的字符串。
  • trim_end_matches<'a, P>(&'a self, pat: P) -> &'a str where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,:返回所有prefix被删除之后的字符串。
  • parse<F>(&self) -> Result<F, <F as FromStr>::Err> where F: FromStr:解析字符串为另一种类型。你可以使用parse::将字符串转换为T类型。
  • is_ascii(&self) -> bool:检查所有的字符是否都是ascii字符。
  • eq_ignore_ascii_case(&self, other: &str) -> bool:ascii字符串忽略大小写比较。
  • make_ascii_uppercase(&mut self):转换字符串为ascii大写的字符。
  • make_ascii_lowercase(&mut self):转换字符串为ascii小写字符。
  • escape_debug(&self) -> EscapeDebug<'_>:指向将转义符作为真真的字符之后的字符串的迭代器。
  • escape_default(self) -> EscapeDefault:指向默认转义符转换为字符串之后的迭代器。
  • escape_unicode(&self) -> EscapeUnicode<'_>:指向将unicode转义符转换为字符串的迭代器。
  • into_boxed_bytes(self: Box<str, Global>) -> Box<[u8], Global>:转换Box为Box不拷贝和分配内存。
  • replace<'a, P>(&'a self, from: P, to: &str) -> String where P: Pattern<'a>:将匹配的pattern替换为另一个字符串。replace将产生一个新的字符串,然后拷贝对应的数据到新的字符串。
  • replacen<'a, P>(&'a self, pat: P, to: &str, count: usize) -> String:和replace一样,不过执行n簇replace操作。
  • to_lowercase(&self) -> String:返回转换为小写之后的字符串,结果为新的字符串。
  • to_uppercase(&self) -> String:返回转换为大写之后的字符串,结果为新的字符串。
  • into_string(self: Box<str, Global>) -> String:转换Box为String而没有任何拷贝和内存分配。
  • repeat(&self, n: usize) -> String:创建一个字符重复n次的String。
  • to_ascii_uppercase(&self) -> String:返回一个对应ascii字符转换为大写之后的String。
  • to_ascii_lowercase(&self) -> String:返回一个对应ascii字符串转换为小写之后的String。

字符串实现的trait

  • Add:add(self, rhs: &'a str) -> <Cow<'a, str> as Add<&'a str>>::Output:实现字符串的加法(拼接)。
  • AddSing:impl<'a> AddAssign<&'a str> for Cow<'a, str>:实现+=操作。
  • AsMut:impl AsMut<str> for String,实现转换为mut。
  • AsRef :实现转换输入类型为T,T为u8、OsStr、Path、str。
  • len_utf8(self) -> usize:返回utf-8编码之后的长度。
  • len_utf16(self) -> usize:返回utf-16编码之后长度
  • encode_utf8(self, dst: &mut [u8]) -> &mut str:返回utf8编码字符后的字符串。
  • encode_utf16(self, dst: &mut [u16]) -> &mut [u16]:返回utf16编码字后的字符串。
  • is_alphabetic(self) -> bool:返回是否为字母
  • ``
  • ``
  • ``
  • ``
  • ``
  • ``
  • ``
  • ``

Sting

常见的构造方式:

  • String::from("hello world"):通过从”hello world”字面量构建String对象。
  • String::from_utf8(vec![240,220]).unwrap();:从uint8数值字符向量读取并转换为String对象。

   转载规则


《Rust基础(字符串)》 bleedingfight 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
Rust三方库-actix Rust三方库-actix
ActixActix 是一个Rust异步Webserver,它提供了构建webserver需要的多种能力,包括路由、中间件、请求预处理、相应后处理等等。所有的actix server均围绕App构建,用于为资源和中间件注册路由。在相同的
2023-09-10
下一篇 
Rust基础(读写文件) Rust基础(读写文件)
Rust文件读写文件读写之前先简单介绍一下rust的两个常见enum。 enum Option<T>{ Some(T), None, } 这里的Option是一个泛型enum,它可以接收任意类型T然后包装其为一个Option
2023-09-10
  目录