Open In App

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

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The ascii() is an inbuilt function in Julia which is used to convert a specified string to a String type and also check the presence of ASCII data. If the 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:

Python




# 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:

Python




# 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads