- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我从外部源接收到一个 json 字符串数组,我的一些字符串看起来像
"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00"
我想过滤掉所有只包含这些空值的字符串。这个我试过了
arr = arr.filter { (str: String) -> Bool in
let invalid = String(data: Data.init(count: str.count), encoding: String.Encoding.utf8)
print(str)
return str == invalid
}
由于这些都是空终止符,我决定创建一个与 str
长度相同但只包含空终止符的空字符串并比较它们。
但这行不通。任何帮助将不胜感激。
最佳答案
我不认为那些是空终止符,因为反斜杠被转义了。这些只是重复多次的四个字符序列 \x00
。
要匹配这样的模式,可以使用正则表达式:
let regex = try! NSRegularExpression(pattern: "^(?:\\\\x00)+$", options: [])
let string = "\\x00\\x00\\x00\\x00\\x00"
// the below will evaluate to true if the string is full of \x00
regex.firstMatch(in: string, options: [], range: NSRange(location: 0, length: string.count)) != nil
关于arrays - 从字符串数组中过滤掉空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51488878/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!