On 2014-05-28, at 11:10 , Aravinda VK <hallimanearav...@gmail.com> wrote:

> Hi,
> 
> How to find number of characters in a string?

Problem 1: define character. Do you mean a glyph? A grapheme cluster? A
code point? Composed or decomposed?

Problem 2: what use is knowing the length of a string?

.len() returns the length of the string in bytes (using its underlying
UTF-8 representation), .char_len() returns the number of non-normalized
code points. AFAIK there is no way to know the number of grapheme
clusters (I don't think there is a Rust implementation of UAX #29) and
glyphs make no sense for text which is not rendered.

> Following example returns byte count instead of number of characters.
> 
>     use std::string::String;
> 
>     fn main() {
>         let unicode_str = String::from_str("ಅ");
>         let ascii_str = String::from_str("a");
>         println!("unicode str: {}, ascii str: {}", unicode_str.len(), 
> ascii_str.len());
>     }
> 
> -- 
> Regards 
> Aravinda | ಅರವಿಂದ 
> http://aravindavk.in
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to