本文共 4202 字,大约阅读时间需要 14 分钟。
In this quick tutorial, we’ll learn how to quickly search a part of a string in an Array. We’ll be demonstrating the examples in Java, Python, and Swift.
在本快速教程中,我们将学习如何快速搜索数组中字符串的一部分。 我们将演示Java,Python和Swift中的示例。
The straightforward way to check whether a substring exists in any of the array elements is looping through the array elements. But in the next sections, we’ll use different approaches which are far shorter, cleaner and readable.
检查子字符串是否存在于任何数组元素中的直接方法是遍历数组元素。 但是在接下来的部分中,我们将使用更短,更清晰,更易读的不同方法。
Using the we can lookup if any substring is present in the array.
使用我们可以查找数组中是否存在任何子字符串。
import java.util.Arrays;import java.util.Optional; public class SubStringChecker{ public static void main(String []args){ System.out.println("Hello World"); String[] array = {"abc","def", "ghi"}; String input = "abcdefghi"; boolean stringExists = substringExistsInArray(input, array); System.out.println(stringExists); String input2 = "acdfhi"; stringExists = substringExistsInArray(input2, array); System.out.println(stringExists); System.out.println(getFirstMatchingSubstring(input, array)); System.out.println(getFirstMatchingSubstring(input2, array)); } public static boolean substringExistsInArray(String inputStr, String[] items) { return Arrays.stream(items).parallel().anyMatch(inputStr::contains); } public static Optional getFirstMatchingSubstring(String inputStr, String[] items) { return Arrays.stream(items).parallel().filter(inputStr::contains).findAny(); }}
In the above code, we’ve created two methods. One to check if the substring exists. Other to return the matching substring.
在上面的代码中,我们创建了两个方法。 一种检查子字符串是否存在。 其他返回匹配的子字符串。
anyMatch
returns any of the substrings that got matched. No particular order.
findAny
returns any of the array elements that got matched. The value is returned as an . anyMatch
返回匹配的任何子字符串。 没有特定的顺序。
findAny
返回任何匹配的数组元素。 该值作为返回。 The output of the above is:
上面的输出是:
We can use to check if the array contains substring or not.
我们可以使用来检查数组是否包含子字符串。
input1 = "abcdefghi"array = ['abc', 'def', 'ghi'] result = any(sub in input1 for sub in array) print("substring exists in the list for input1: " + str(result)) input2 = "acdfhi"result = any(sub in input2 for sub in array) print("substring exists in the list for input2: " + str(result)) matchingString = next(substring for substring in array if substring in input1)print("substring that matched was "+matchingString)#Output"""substring exists in the list for input1: True substring exists in the list for input2: False substring that matched was abc """
any
returns true if any of the substrings is present in the array.
如果数组中存在任何子字符串,则any
返回true。
To print the matched substring we use next
.
要打印匹配的子字符串,我们使用next
。
next
throws StopIteration if the condition was not matched at all.
如果条件根本不匹配,则next
抛出StopIteration。
Swift has been increasingly gaining popularity.
Swift已经越来越受欢迎。
The below code snippet is a validation of that.
下面的代码段对此进行了验证。
import UIKitlet input1 = "abcdefghi"let array = ["abc", "def", "ghi"]let input1Matches = array.contains(where: input1.contains)print("array contains input1 \(input1Matches)")let input2 = "acdfhi"let input2Matches = array.contains(where: input2.contains)print("array contains input2 \(input2Matches)")
array.contains(where: string.contains)
returns us a boolean if the array contains a substring of the string.
如果数组包含字符串的子字符串,则array.contains(where: string.contains)
返回一个布尔值。
The output of the above code is :
上面代码的输出是:
That sums up this tutorial. We have covered an interesting problem in Java, Python, and Swift.
总结了本教程。 我们已经介绍了Java,Python和Swift中一个有趣的问题。
翻译自:
转载地址:http://jmlzd.baihongyu.com/