Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a String if it's an ASCII or not?

For example something like:

"ASCII".is_ascii? # => true

"تجربة".is_ascii? # => false
like image 435
Amer Avatar asked Sep 26 '10 11:09

Amer


People also ask

How do I check if a string contains only ASCII characters?

The isascii() function returns a boolean value where True indicates that the string contains all ASCII characters and False indicates that the string contains some non-ASCII characters.

How do you identify ASCII?

To identify a character's ASCII value, it is common to look it up on an ASCII table. The ASCII table pairs each character to its assigned value between 0 and 127.

How do you check if a string has a char?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.

Which function is used to check if all characters in a string conform to ASCII?

The isascii() function returns True if all characters in a string are included in ASCII.


1 Answers

There is a bult-in Ruby string method right for you.

str.ascii_only? # → true or false

Returns true for a string which has only ASCII characters.

"abc".force_encoding("UTF-8").ascii_only?          #=> true
"abc\u{6666}".force_encoding("UTF-8").ascii_only?  #=> false
like image 85
Simon Perepelitsa Avatar answered Oct 16 '22 02:10

Simon Perepelitsa