在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"