Anonymous
Anonymous

Reputation: 21

Replacing multiple characters from a single string in VB

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

mary had a little lamb

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?

Upvotes: 2

Views: 8914

Answers (1)

Andrew Morton
Andrew Morton

Reputation: 25066

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"

Upvotes: 5

Related Questions