views:

24

answers:

2

In perl you can write

$string =~ tr/[a,e,i,o,u,y]/[A,E,I,O,U,Y]/;

for example.

Is it possible to achieve the same "translation" effects with VB.Net regexes?

Thanks you!

PS: I'm not searching for a way to port this very example, it's more of a curiosity question :)

+1  A: 

There is no standard method for this. You can do it by iterating over each character in your input string and using a dictionary to map it to another character (or leave it unchanged if the character is not found in the dictionary). The result can be built using a StringBuilder for performance reasons.

If performance is not an issue then you might be able to use a few replace operations instead:

s = s.Replace("a", "A")
     .Replace("e", "E")
     ...
     .Replace("y", "Y");
Mark Byers
As I said, I'm not trying to replicate the behaviour of this very example, but I just wanted to now if there was a similar construct :)
CFP
@CFP: Then the answer is no, but you can write one yourself using the method I described in my answer (first paragraph).
Mark Byers
A: 

Here's one way to do this:

public string fakeTR(string theString, char[] org, char[] rep)
{
  for(int i=0;i<org.lenght;i++)
  {
    theString = theString.Replace(org[i], rep[i]);
  }
  return theString;
}

You would be able to call it with somewhat clunky but shorter:

string v = "Black in South Dakota";
v = fakeTR(v, new char[]{'B','l','a','c','k'}, new char[]{'W','h','i','t','e'}); 

H/T http://discuss.joelonsoftware.com/default.asp?dotnet.12.306220.6

DVK