在VB中替换单个字符串中的多个字符 [英] Replacing multiple characters from a single string in VB

查看:127
本文介绍了在VB中替换单个字符串中的多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Visual Basic 中替换字符串中的多个字符,例如:

Is it possible to replace multiple characters from a string in Visual Basic, like for example:

mary had a little lamb

所有字母a必须改为z,所有的 m 必须改为 y,所有 t 必须改为 x只需一行代码?

All letter a must be changed to z, all m must be changed to y, all t must be changed to x in just one line of code?

推荐答案

由于Replace的结果是一个字符串,可以串联多个替换:

As the result of Replace is a string, you can concatenate multiple replacements:

Dim s As String = "mary had a little lamb"
Dim t As String = s.Replace("a", "z").Replace("m", "y").Replace("t", "x")
Console.WriteLine(t) ' outputs "yzry hzd z lixxle lzyb"

这篇关于在VB中替换单个字符串中的多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆