在 Julia 中检查字符串是否包含非 ASCII 值 – ascii() 方法
ascii()
是 julia 中的内置函数,用于将指定的字符串转换为 String 类型,并检查 ASCII 数据的存在。如果存在非 ASCII 字节,则抛出 ArgumentError 指示第一个非 ASCII 字节的位置。
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.
示例 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"))
输出:
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
示例 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"))
输出:
GeeksforGeeks