如何在Rust中从特定索引开始的子字符串中查找? [英] How to find in a substring starting at a specific index in Rust?
本文介绍了如何在Rust中从特定索引开始的子字符串中查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Rust的标准库中是否有一个 find
函数,该函数从字符串中的给定索引开始搜索子字符串?就像 indexOf 代码>
在JavaScript中.
Is there a find
function in Rust's standard library which searches for a substring starting at a given index in the string? Just like indexOf
in JavaScript.
推荐答案
You would use str::find
on a substr and then add the offset back:
let s = "foobarfoo";
let index: Option<usize> = s[4..].find("foo").map(|i| i + 4);
println!("{:?}", index);
Some(6)
这篇关于如何在Rust中从特定索引开始的子字符串中查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文