删除字符串中的元音字母
时间: 2023-05-31 15:02:09 浏览: 47
以下是Python的代码实现:
```python
def remove_vowels(string):
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
result = ""
for char in string:
if char not in vowels:
result += char
return result
```
示例:
```python
print(remove_vowels("Hello World")) # 输出 "Hll Wrld"
```
相关问题
c++删除字符串中的元音字母
以下是Python代码实现:
```python
def remove_vowels(string):
vowels = "aeiouAEIOU"
return "".join([char for char in string if char not in vowels])
```
例子:
```python
>>> remove_vowels("Hello World")
'Hll Wrld'
>>> remove_vowels("Python is awesome")
'Pythn s wsm'
```
从字符串中删除元音 js
可以使用正则表达式和字符串的 replace 方法来删除元音字母。以下是一个示例代码:
```js
function removeVowels(str) {
return str.replace(/[aeiou]/gi, '');
}
console.log(removeVowels('hello world')); // 输出 "hll wrld"
```
在这个函数中,我们使用了正则表达式 `/[aeiou]/gi` 来匹配所有的元音字母,其中 `g` 表示全局匹配,`i` 表示忽略大小写。然后我们使用字符串的 `replace` 方法来将所有匹配的元音字母替换成空字符串。最后返回处理后的字符串。