数组字符串过滤掉包含MP4字符串的
时间: 2024-06-15 19:01:29 浏览: 12
在编程中,如果你有一个数组或字符串列表,想要过滤掉那些包含特定子串(如 "MP4")的元素,你可以使用循环遍历和条件判断来完成这个任务。这里是一个简单的Python示例:
```python
def filter_strings(array, substring):
return [item for item in array if substring not in item]
# 示例数组
string_array = ['file.mp4', 'image.jpg', 'document.pdf', 'audio.m4a']
# 过滤掉包含 'MP4' 的元素
filtered_array = filter_strings(string_array, 'MP4')
print(filtered_array) # 输出: ['image.jpg', 'document.pdf']
```
在这个例子中,`filter_strings` 函数接受一个数组 `array` 和一个子串 `substring`,然后返回一个新的数组,其中不包含 `substring` 的元素。
相关问题
php过滤数组字符串空格
你可以使用 PHP 中的 `array_map()` 函数以及 `trim()` 函数来过滤数组中的字符串空格。示例代码如下:
```php
// 假设 $arr 是一个包含字符串的数组
$arr = [' hello ', 'world ', ' foo bar '];
// 使用 array_map() 函数和 trim() 函数过滤数组中的字符串空格
$arr = array_map('trim', $arr);
// 输出过滤后的数组
print_r($arr);
```
输出:
```
Array
(
[0] => hello
[1] => world
[2] => foo bar
)
```
在上述示例代码中,`trim()` 函数用于去除字符串两侧的空格,`array_map()` 函数用于对数组中的每个元素都应用 `trim()` 函数。
java中split返回的数组元素过滤空字符串
在 Java 中,可以使用 split 方法将一个字符串按照指定的分隔符分割成多个子字符串,返回的是一个字符串数组。如果要过滤掉数组中的空字符串,可以使用 Java 8 中的 Stream API 和 filter 方法来实现。
具体操作如下:
```java
String str = "hello,,world,,";
String[] arr = str.split(",");
String[] result = Arrays.stream(arr)
.filter(s -> !s.isEmpty())
.toArray(String[]::new);
```
上述代码中,首先使用 split 方法将字符串按照逗号分隔成字符串数组 arr。接着使用 Arrays 类的 stream 方法将数组转换成一个流。然后使用 filter 方法过滤掉空字符串,最后使用 toArray 方法将过滤后的结果转换成一个字符串数组 result。
注意:如果要过滤掉的不仅是空字符串,还包括只含有空格的字符串,可以使用 trim 方法先将字符串两端的空格去掉,然后再进行过滤。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)