Answer the question
In order to leave comments, you need to log in
Analog of php function strtr in .Net
Can you please tell me how to perform such a function in .Net?
For example
$a=array("o" => "l", "l" => "o");
$b=strtr("ololo",$a);
Answer the question
In order to leave comments, you need to log in
using System;
using System.Text;
using System.Collections.Generic;
class Program
{
public static void Main(string[] args)
{
var repl = new Dictionary<char, char> { {'o', 'l'}, {'l','o'} };
var src = "ololo";
Console.WriteLine("Source string: " + src + "; changed string: " + src.Strtr(repl));
Console.ReadKey();
}
}
public static class StringExt
{
public static string Strtr(this string src, Dictionary<char, char> replacePairs) {
var sb = new StringBuilder();
foreach (char c in src) {
if(replacePairs.ContainsKey(c)) {
sb.Append(replacePairs[c]);
} else {
sb.Append(c);
}
}
return sb.ToString();
}
}
There are several options, the most useful for you is through regexps. Deal with them and then it will come in handy. Regex.Replace and further as you fantasize.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question