Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you iterate over a string by character

I have a string and I need to scan for every occurrence of "foo" and read all the text following it until a second ". Since Rust does not have a contains function for strings, I need to iterate by characters scanning for it. How would I do this?

Edit: Rust's &str has a contains() and find() method.

like image 218
user2171584 Avatar asked Mar 01 '14 18:03

user2171584


People also ask

Can you use a for loop to iterate over the characters in a string?

Iterate over string using for loopIterating over the string is simple using for loop and in operator i.e. sampleStr = "Hello!!"

Can you iterate over strings?

You can traverse a string as a substring by using the Python slice operator ([]). It cuts off a substring from the original string and thus allows to iterate over it partially. To use this method, provide the starting and ending indices along with a step value and then traverse the string.

How do you go through a character in a string C++?

Program to loop on every character in string in C++ To loop on each character, we can use loops starting from 0 to (string length – 1). For accessing the character we can either use subscript operator "[ ]" or at() function of string object.


2 Answers

I need to iterate by characters scanning for it.

The .chars() method returns an iterator over characters in a string. e.g.

for c in my_str.chars() {      // do something with `c` }  for (i, c) in my_str.chars().enumerate() {     // do something with character `c` and index `i` } 

If you are interested in the byte offsets of each char, you can use char_indices.

Look into .peekable(), and use peek() for looking ahead. It's wrapped like this because it supports UTF-8 codepoints instead of being a simple vector of characters.

You could also create a vector of chars and work on it from there, but that's more time and space intensive:

let my_chars: Vec<_> = mystr.chars().collect(); 
like image 199
centaurian_slug Avatar answered Oct 14 '22 02:10

centaurian_slug


The concept of a "character" is very ambiguous and can mean many different things depending on the type of data you are working with. The most obvious answer is the chars method. However, this does not work as advertised. What looks like a single "character" to you may actually be made up of multiple Unicode code points, which can lead to unexpected results:

"a̐".chars() // => ['a', '\u{310}'] 

For actual string processing, you want to work with graphemes. A grapheme consists of one or more unicode code points represented as a string slice. These map better to the human perception of "characters". To create an iterator of graphemes, you can use the unicode-segmentation crate:

use unicode_segmentation::UnicodeSegmentation;  for grapheme in my_str.graphemes(true) {     // ... } 

If you are working with raw ASCII then none of the above applies to you, and you can simply use the bytes iterator:

for byte in my_str.bytes() {     // ... } 

Although, if you are working with ASCII then arguably you shouldn't be using String/&str at all and instead use Vec<u8>/&[u8] or the ascii crate.

like image 29
Ibraheem Ahmed Avatar answered Oct 14 '22 03:10

Ibraheem Ahmed