Reputation: 539
Hi all a have a program working but return string like ")Hi("
it should be "(Hi)"
so i need to replace '('
with ')'
and replace
')'
with '('
it sounds easy
s.Replace('(',')').Replace(')','(')
the trick is that after the first replace the string change from
")Hi("
to "(Hi("
after the second the replace will change both characters back
the final will become ")Hi)"
Help Please
Upvotes: 2
Views: 5898
Reputation: 29647
You could also use a regex replacement.
s = System.Text.RegularExpressions.Regex.Replace(s, @"[)](\w+)[(]", "($1)");
Upvotes: 2
Reputation: 22876
Regex.Replace
lets you process each Match
(needs using System.Text.RegularExpressions;
) :
string result = Regex.Replace(")Hi(", @"\(|\)", m => m.Value == "(" ? ")" : "(");
Alternative can be replacing one of the characters with something else:
string result = ")Hi(".Replace('(', '\0').Replace(')', '(').Replace('\0', ')');
Upvotes: 0
Reputation: 186
Method 1) Use the following:
var requiredString = string.Join(string.Empty, str.Select(x=>{if (x == '(') x = ')'; else if (x == ')') x = '('; return x;}));
Method 2) Or you can Use following Extension Method:
public static class StringExtensions
{
public static string ReplaceMultiple(this string source, Dictionary<char, char> replacements)
{
return string.Join(string.Empty , source.Select(x=>Replace(x,replacements)));
}
private static char Replace(char arg, Dictionary<char, char> replacements)
{
if(replacements.ContainsKey(arg))
arg = replacements[arg];
return arg;
}
}
This method can be used as follows:
var rep = new Dictionary<char, char>
{
{ ')', '(' },
{ '(', ')' },
// { '#', '*' },
// { '*', '#' }
};
var c = str.ReplaceMultiple(rep);
Upvotes: 0
Reputation: 14231
var s = ")Hi(";
var sb = new StringBuilder();
foreach (var c in s)
if (c == ')')
sb.Append('(');
else if (c == '(')
sb.Append(')');
else
sb.Append(c);
s = sb.ToString();
Upvotes: 0
Reputation: 216243
You cannot use Replace because it works its replacement operation on the whole string, not char by char.
A simple brute force solution could be this one
void Main()
{
// A dictionary where every key points to its replacement char
Dictionary<char, char> replacements = new Dictionary<char, char>()
{
{'(', ')'},
{')', '('},
};
string source = ")Hi(";
StringBuilder sb = new StringBuilder();
foreach (char c in source)
{
char replacement = c;
if(replacements.ContainsKey(c))
replacement = replacements[c];
sb.Append(replacement,1);
}
Console.WriteLine(sb.ToString());
}
You can transform this in an extension method adding to a static class
public static class StringExtensions
{
public static string ProgressiveReplace(this string source, Dictionary<char, char> replacements)
{
StringBuilder sb = new StringBuilder();
foreach (char c in source)
{
char replacement = c;
if (replacements.ContainsKey(c))
replacement = replacements[c];
sb.Append(replacement, 1);
}
return sb.ToString();
}
}
and call it from your code with
Dictionary<char, char> replacements = new Dictionary<char, char>()
{{'(', ')'},{')', '('}};
s = s.ProgressiveReplace(replacements);
Upvotes: 0