4

I have a simple C# console code, which converts html name to their corresponding symbols.

Example :- €-> this is euro html name , &#8364 ->. this is decimal code for euro;

My code will convert this name to euro symbol-> €

But when I am converting &#8377 to ₹ , it not working.

My Console Application code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Multiple_Replace
{
    class Program
    {
        static void Main(string[] args)
        {
        var input = " ₹ 5000 €";
        var replacements = new Dictionary<string, string> { { "₹", "&#8377;" }, {"&euro;", "&#8364;"} };
        var output = replacements.Aggregate(input, (current, replacement) => current.Replace(replacement.Key, replacement.Value));
        Console.WriteLine(output);
        Console.ReadLine();
        }
    }
}

Please help me out.

ib11
  • 2,530
  • 3
  • 22
  • 55
Sanjiv
  • 980
  • 2
  • 11
  • 29
  • 1
    Could you please define `it not working.` and your `input` already contains our currency symbol then what you are trying to replace? – sujith karivelil Jun 22 '16 at 07:53
  • There is a syntax error in the Dictionary enumeration: ` { { "₹", "₹" }, {"€"}, "€" };` – ib11 Jun 22 '16 at 07:55
  • @un-lucky:- I am trying to show ₹ symbol in my project. I have explain this code in `example` above. if you run this code it will convert html name to their symbol (eg: €). For € its working fine but for ₹ its not working. – Sanjiv Jun 22 '16 at 07:58
  • @ib11:- I have updated my question with full code. Please it out. – Sanjiv Jun 22 '16 at 08:02
  • Your code is converting ₹ to "₹". Is this what you want it to do? – H77 Jun 22 '16 at 08:03
  • @Sanjiv: Instead of converting `"₹"` to `"₹"` you are doing the Reverse process through your code. Is that what you mean by `not working`? – sujith karivelil Jun 22 '16 at 08:04
  • I want to show ₹ symbol using this code.. – Sanjiv Jun 22 '16 at 08:04
  • 1
    @Sanjiv the symbol is already in the input? – H77 Jun 22 '16 at 08:05
  • When we give ₹ symbol to our input, output will show ₹ symbol. Thats my requirement. – Sanjiv Jun 22 '16 at 08:06
  • but its not showing in output.. – Sanjiv Jun 22 '16 at 08:06
  • @ib11:- simply copy & paste my code in your local console application and run, then you understand my requirement. – Sanjiv Jun 22 '16 at 08:08
  • See my answer below. The **euro** does not work either. You just don't have it in the string, That's all. – ib11 Jun 23 '16 at 01:45

2 Answers2

1

First, I think there is a more basic issue here.

Your input string does not contain the string "&euro;".

If you change the Dictionary to this:

var replacements = new Dictionary<string, string> { { "₹", "&#8377;" }, { "€", "&#8364;" } };

Then you will see that the output is in fact:

&#8377; 5000 &#8364;

So you are not seeing what you think you see because while the "₹" is part of the string the "&euro;" is not.


That said, reading up on this, it seems that this html entity code for the Rupee symbol is not supported by all browsers.

The following code works using the unicode code if this is what you want to accomplish:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Multiple_Replace
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = " ₹ 5000 €";
            var replacements = new Dictionary<string, string> { { "₹", "\u20B9" }, { "€", "\u20AC" } };
            var output = replacements.Aggregate(input, (current, replacement) => current.Replace(replacement.Key, replacement.Value));
            Console.WriteLine(output);
            Console.ReadLine();
        }
    }
}

Also, check this question and the accepted answer, I think this will be informative for you:

Displaying the Indian currency symbol on a website

And finally if you want a correct representation of the symbol in an html, you can use this code:

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

<i class="fa fa-inr"></i>

HTH

Community
  • 1
  • 1
ib11
  • 2,530
  • 3
  • 22
  • 55
0

Have you tried setting the console output encoding?

e.g.

Console.OutputEncoding = System.Text.Encoding.UTF8;
H77
  • 5,859
  • 2
  • 26
  • 39
  • :- Actually i have already implemented my code for other symbol (!, @, #, $, %, ^, &, *, etc), its working for all of them, Only problem with the ₹ symbol. If possible please tell some solution on above code. – Sanjiv Jun 22 '16 at 08:11
  • My Output is:-> ₹ 5000. Instead of ₹ symbol, its printing its html code(i.e:- ₹) – Sanjiv Jun 22 '16 at 08:23
  • 1
    In your replace dictionary you're replacing "€" and not €. Why is that? – H77 Jun 22 '16 at 08:26
  • beacuse € has &euro html name, but for ₹ i didn't find any html name for that. so i used directly ₹ symbol. – Sanjiv Jun 22 '16 at 08:28
  • 1
    string.replace doesn't replace html names. so the code above will output the euro symbol that was in the input instead of doing any replace. – H77 Jun 22 '16 at 08:31