Skip to content
Related Articles

Related Articles

Improve Article

Check if a string contains a non-ASCII value in Julia – ascii() Method

  • Last Updated : 26 Mar, 2020

The ascii() is an inbuilt function in julia which is used to convert a specified string to String type and also check the presence of ASCII data. If non-ASCII byte is present, throw an ArgumentError indicating the position of the first non-ASCII byte.

Syntax:
ascii(s::AbstractString)

Parameters:

  • s::AbstractString: Specified string

Returns: It returns the converted string if it contain ASCII byte else throw an ArgumentError indicating the position of the first non-ASCII byte.

Example 1:






# Julia program to illustrate 
# the use of String ascii() method
  
# Checking the presence of ASCII byte else 
# throw an ArgumentError indicating the
# position of the first non-ASCII byte.
  
# In below string, ? is the non-ASCII byte
println(ascii("GFG?GFG"))

Output:

ERROR: LoadError: ArgumentError: invalid ASCII at index 4 in "GFG?GFG"
Stacktrace:
 [1] ascii(::String) at ./strings/util.jl:479
while loading /home/cg/root/421147/main.jl, in expression starting on line 9

Example 2:




# Julia program to illustrate 
# the use of String ascii() method
  
# Checking the presence of ASCII byte else 
# throw an ArgumentError indicating the
# position of the first non-ASCII byte.
  
# In below string, non-ASCII byte is not present
println(ascii("GeeksforGeeks"))

Output:

GeeksforGeeks
My Personal Notes arrow_drop_up
Recommended Articles
Page :