简体   繁体   English

检查未修剪字符串列表是否包含修剪字符串

[英]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.

相关问题 Java,如果列表中的修剪字符串包含字符串,则返回 - Java, return if trimmed String in List contains String 如何检查文本文件中是否包含单个字符串或两个字符串? - How to check if a text file contain a single string or two strings? 从字符串数组中获取包含指定字符的字符串列表 - Get a list of strings that contain a specified character from an array of string 检查字符串是否包含列表中的任何字符串 - Check if a string contains any of the strings from an List 我可以检查字符串以包含我没有列出的字符吗? - Can I check string to contain character that I didn't list? 匹配两个字符串列表其中一个列表可以包含带有通配符“?”的字符串 - Match two List of String Where one List can contain strings with wildcard character '?' 如何检查文件夹中文件名中的元素是否在字符串数组列表中包含字符串名? - How to check if elements in a file names in a folder contain string names in an arraylist of strings? 如何在修剪后的字符串上添加“…”? - How to add “…” on a trimmed string? 检查用户输入的字符串是否包含相同的字母 - Check if user input strings contain the same letters Java - 检查 under_score 字符串是否在 lowerCamel 字符串列表中 - Java - check if under_score string is in a list of lowerCamel strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM