给你一列字符串数组scanStr[]和字符串s,怎么在scanStr[]查出含s子字符串的字符串,给出详细代码
时间: 2024-05-12 09:20:57 浏览: 14
以下是一个基于Python语言的示例代码,用于在字符串数组中查找包含子字符串的字符串:
```python
def find_strings_with_substring(scanStr, s):
result = []
for string in scanStr:
if s in string:
result.append(string)
return result
```
这个函数接受两个参数,分别是字符串数组`scanStr`和子字符串`s`,并返回一个包含包含子字符串的字符串的新数组。它使用一个for循环遍历输入的字符串数组,对每个字符串使用`in`操作符检查是否包含子字符串。如果包含,就将该字符串添加到结果数组中。最后,返回结果数组。
你可以按以下方式调用这个函数:
```python
scanStr = ["hello world", "goodbye", "how are you doing?"]
s = "o"
result = find_strings_with_substring(scanStr, s)
print(result)
```
这将输出包含子字符串`"o"`的字符串数组:
```
['hello world', 'goodbye']
```
相关问题
给你一列字符串数组scanStr[]和字符串s,怎么在scanStr[]查出包含s的字符串
可以使用循环遍历每个字符串,判断该字符串是否包含字符串s,如果包含则将该字符串添加到结果数组中。
示例代码:
```
public static String[] findString(String[] scanStr, String s) {
List<String> result = new ArrayList<>();
for (String str : scanStr) {
if (str.contains(s)) {
result.add(str);
}
}
return result.toArray(new String[0]);
}
```
使用示例:
```
String[] scanStr = {"hello world", "goodbye", "hello there"};
String s = "hello";
String[] result = findString(scanStr, s);
System.out.println(Arrays.toString(result)); // ["hello world", "hello there"]
```
给你一列字符串数组scanStr[]和字符串s,怎么在scanStr[]查出含s子字符串的字符串,给出详细java代码
```
public static void searchSubstring(String[] scanStr, String s) {
for (String str : scanStr) {
if (str.contains(s)) {
System.out.println(str);
}
}
}
```
调用方式:
```
String[] scanStr = {"hello world", "world hello", "java is great"};
String s = "hello";
searchSubstring(scanStr, s);
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)