输入两个字符串,从第一个字符串中删除第二个字符串中的所有字符。 例如输入"They are students."和"aeiou",则删除之后的第一个字符串变成"Thy r stdnts."
时间: 2023-07-19 07:17:44 浏览: 50
好的,这是一个字符串处理的问题,可以用 Python 语言来实现。可以使用两个 for 循环,遍历第一个字符串中的每个字符,如果该字符不在第二个字符串中,则将其添加到一个新的字符串中。最后返回新的字符串即可。具体实现可以参考以下代码:
```python
def delete_chars(s1, s2):
new_s = ''
for c1 in s1:
if c1 not in s2:
new_s += c1
return new_s
s1 = 'They are students.'
s2 = 'aeiou'
new_s = delete_chars(s1, s2)
print(new_s)
```
输出结果为:
```
Thy r stdnts.
```
希望能解决您的问题。
相关问题
输入两个字符串,从第一个字符串中删除第二个字符串中的所有字符。例如,输入“they are students.”和“aeiou”,则删除之后的第一个字符串变成“thy r stdnts”。
题目要求输入两个字符串,从第一个字符串中删除第二个字符串中所有的字符。举个例子,输入 "they are students." 和 "aeiou",则删除后的第一个字符串变成 "thy r stdnts"。
编程实现删除指定字符串,输入两个字符串,从第一个字符串中删除第二个字符串中的所有字符。例如,输入“They are students.”和“aeiou”,则删除之后的第一个字符串变成“Thy r stdnts.”
以下是 Python 代码实现:
```python
def delete_chars(s1, s2):
for c in s2:
s1 = s1.replace(c, '')
return s1
s1 = "They are students."
s2 = "aeiou"
result = delete_chars(s1, s2)
print(result) # 输出 "Thy r stdnts."
```
首先定义一个 `delete_chars` 函数,接收两个字符串参数 `s1` 和 `s2`。然后遍历 `s2` 中的每个字符,使用 `replace` 方法从 `s1` 中删除所有匹配的字符。最后返回修改后的 `s1`。
在主程序中定义输入的两个字符串 `s1` 和 `s2`,然后调用 `delete_chars` 函数,并将结果存储在 `result` 变量中。最后输出 `result`。