For example something like:
"ASCII".is_ascii? # => true
"تجربة".is_ascii? # => false
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.
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.
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.
The isascii() function returns True if all characters in a string are included in ASCII.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With