[英]Check if List of untrimmed Strings contain trimmed String
We have a list of Strings which might have whitespaces at their beginning/end 我们有一个字符串列表,它们的开头/结尾可能有空格
We would like to check whether this list contains a string which is definitely trimmed, ie listContainingUntrimmedStrings.contains(trimmedString)
我们想检查这个列表是否包含一个绝对修剪过的字符串,即
listContainingUntrimmedStrings.contains(trimmedString)
Is there a concise, preferably one-line way to do this, or we just cannot spare copying the untrimmed list into a trimmed one and perform the contains check on that / loop on the untrimmed list and do trimmed equality check on the elements one-by-one / other verbose solution? 是否有一个简洁的,最好是单行的方式来做到这一点,或者我们只是无法将未修剪的列表复制到修剪过的列表中并对未修剪的列表上的/ loop执行包含检查,并对元素进行修剪等式检查 - by-one / other verbose解决方案?
You can do that in a functional style using stream operations: 您可以使用流操作以功能样式执行此操作:
listContainingUntrimmedStrings.stream()
.map(String::trim) // lazily apply trimming to each element in the list
.anyMatch(trimmedString::equals) // check if any of those elements equals your initial string (assuming that `trimmedString` is NOT NULL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.