📜  在 Julia 中检查字符串包含非 ASCII 值 – ascii() 方法

📅  最后修改于: 2021-11-25 04:42:23             🧑  作者: Mango

ascii()是 julia 中的一个内置函数,用于将指定的字符串转换为 String 类型并检查 ASCII 数据的存在。如果存在非 ASCII 字节,则抛出一个 ArgumentError 指示第一个非 ASCII 字节的位置。

示例 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