博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
检查数组中的子字符串– Java,Python和Swift
阅读量:2535 次
发布时间:2019-05-11

本文共 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.

检查子字符串是否存在于任何数组元素中的直接方法是遍历数组元素。 但是在接下来的部分中,我们将使用更短,更清晰,更易读的不同方法。

使用Java数组中的子字符串 (Substring in an Array Using Java)

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.

Similarly, findAny returns any of the array elements that got matched. The value is returned as an .

anyMatch返回匹配的任何子字符串。 没有特定的顺序。

同样, findAny返回任何匹配的数组元素。 该值作为返回。

The output of the above is:

上面的输出是:

Check For Substrings In Array Java

Check For Substrings In Array Java

检查数组Java中的子字符串

使用Python检查数组中的子字符串 (Checking for Substring in Array Using Python)

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检查数组是否包含子字符串 (Using Swift to check if array contains substring)

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 :

上面代码的输出是:

Check For Substrings In Array Swift

Check For Substrings In Array Swift

检查数组Swift中的子字符串

That sums up this tutorial. We have covered an interesting problem in Java, Python, and Swift.

总结了本教程。 我们已经介绍了Java,Python和Swift中一个有趣的问题。

翻译自:

转载地址:http://jmlzd.baihongyu.com/

你可能感兴趣的文章
影响系统性能的20个瓶颈--转自开源中国
查看>>
根据ISBN获取豆瓣API提供的图书信息
查看>>
【转】Python中*args和**kwargs的区别
查看>>
git命令简单使用
查看>>
CODEFORCES 125E MST Company 巧用Kruskal算法
查看>>
C++标准库分析总结(三)——<迭代器设计原则>
查看>>
Ibatis 配置问题
查看>>
Notability 3.1 for Mac 中文共享版 – 好用的文本笔记工具
查看>>
HDU 2089 数位dp入门
查看>>
How do I resolve the CodeSign error: CSSMERR_TP_NOT_TRUSTED?
查看>>
linux下添加定时任务
查看>>
python的第三篇--安装Ubuntu、pycharm
查看>>
LeetCode 1092. Shortest Common Supersequence
查看>>
《区块链技术与应用》北京大学肖臻老师公开课 笔记
查看>>
139句
查看>>
购买《哈利波特》书籍
查看>>
谈谈一些网页游戏失败的原因到底有哪些?(转)
查看>>
备案的问题
查看>>
asp.net下ajax.ajaxMethod使用方法
查看>>
win10+mongodb安装配置
查看>>