rust - Find a string starting from given index -
what correct way how find substring if need start not 0?
i have code:
fn splitfile(reader: bufreader<file>) { line in reader.lines() { let mut l = line.unwrap(); // l contains "06:31:53.012 index0:2015-01-06 00:00:13.084 ...
i need find third :
, parse date behind it. still no idea how it, because find
doesn't have param begin
- see https://doc.rust-lang.org/std/string/struct.string.html#method.find.
(i know can use regex. have done, i'd compare performance - whether parsing hand might quicker using regex.)
you right, there doesn't appear trivial way of skipping several matches when searching string. can hand though.
fn split_file(reader: bufreader<file>) { line in reader.lines() { let mut l = &line.as_ref().unwrap()[..]; // slice _ in 0..3 { if let some(idx) = l.find(":") { l = &l[idx+1..] } else { panic!("the line didn't have enough colons"); // shouldn't panic } } // l contains date ...
update:
as faiface points out below, can bit cleaner splitn()
:
fn split_file(reader: bufreader<file>) { line in reader.lines() { let l = line.unwrap(); if let some(datetime) = l.splitn(4, ':').last() { // datetime contains timestamp string ... } else { panic!("line doesn't contain timestamp"); } } }
you should go upvote answer.
Comments
Post a Comment