gpt4 book ai didi

r - 检测字符串中的非 ascii 字符

转载 作者:行者123 更新时间:2023-12-03 11:36:01 24 4
gpt4 key购买 nike

如何以类似 grep 的方式检测字符串向量中的非 ascii 字符。例如下面我想返回 c(1, 3)c(TRUE, FALSE, TRUE, FALSE) :

x <- c("façile test of showNonASCII(): details{", 
"This is a good line", "This has an ümlaut in it.", "OK again. }")
试图:
y <- tools::showNonASCII(x)
str(y)
p <- capture.output(tools::showNonASCII(x))

最佳答案

另一种可能的方法是尝试将您的字符串转换为 ASCII 并尝试检测所有无法转换的生成的不可打印控制字符

grepl("[[:cntrl:]]", stringi::stri_enc_toascii(x))
## [1] TRUE FALSE TRUE FALSE

虽然看起来 stringi也有这种类型的东西的内置功能
stringi::stri_enc_mark(x)
# [1] "latin1" "ASCII" "latin1" "ASCII"

关于r - 检测字符串中的非 ascii 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34613761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com