如果我不需要从0开始,如何找到子串的正确方法是什么?
我有这个代码:
fn SplitFile(reader: BufReader<File>) {
for line in reader.lines() {
let mut l = line.unwrap();
// l contains "06:31:53.012 index0:2015-01-06 00:00:13.084
...
Run Code Online (Sandbox Code Playgroud)
我需要找到第三个:并解析它背后的日期.仍然不知道该怎么做,因为find没有任何类似的参数begin- 请参阅https://doc.rust-lang.org/std/string/struct.String.html#method.find.
(我知道我可以使用正则表达式.我已经完成了,但我想比较性能 - 手动解析是否比使用正则表达式更快.)
在我看来,这个问题有一个更简单的解决方案,那就是使用一种.splitn()方法.此方法最多n次按给定模式拆分字符串.例如:
let s = "ab:bc:cd:de:ef".to_string();
println!("{:?}", s.splitn(3, ':').collect::<Vec<_>>());
// ^ prints ["ab", "bc", "cd:de:ef"]
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您需要将行拆分为4个部分,':'并取第4个部分(从0开始索引):
// assuming the line is correctly formatted
let date = l.splitn(4, ':').nth(3).unwrap();
Run Code Online (Sandbox Code Playgroud)
如果您不想使用unwrap(行可能未正确格式化):
if let Some(date) = l.splitn(4, ':').nth(3) {
// parse the date and time
}
Run Code Online (Sandbox Code Playgroud)
你是对的,在搜索字符串时似乎没有任何简单的方法可以跳过多个匹配项。不过你可以手工完成。
fn split_file(reader: BufReader<File>) {
for line in reader.lines() {
let mut l = &line.as_ref().unwrap()[..]; // get a slice
for _ in 0..3 {
if let Some(idx) = l.find(":") {
l = &l[idx+1..]
} else {
panic!("the line didn't have enough colons"); // you probably shouldn't panic
}
}
// l now contains the date
...
Run Code Online (Sandbox Code Playgroud)
更新:
正如下面faiface所指出的,您可以使用以下方法更干净地完成此操作:splitn()
fn split_file(reader: BufReader<File>) {
for line in reader.lines() {
let l = line.unwrap();
if let Some(datetime) = l.splitn(4, ':').last() {
// datetime now contains the timestamp string
...
} else {
panic!("line doesn't contain a timestamp");
}
}
}
Run Code Online (Sandbox Code Playgroud)
你应该为他的回答点赞。
| 归档时间: |
|
| 查看次数: |
644 次 |
| 最近记录: |