【问题标题】:Converting Indian Currency Symbol转换印度货币符号
【发布时间】:2016-10-24 00:30:01
【问题描述】:

我有一个简单的 C# 控制台代码,它将 html 名称转换为相应的符号。

Example :- €-> 这是欧元 html 名称,&#8364 ->。这是欧元的十进制代码;

我的代码会将此名称转换为欧元符号-> €

但是当我将 ₹ 转换为 ₹ 时,它不起作用。

我的控制台应用程序代码:

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();
        }
    }
}

请帮帮我。

【问题讨论】:

  • 能否请您定义it not working. 并且您的input 已经包含我们的货币符号,那么您要替换什么?
  • 字典枚举中有语法错误:` { { "₹", "₹" }, {"€"}, "€" };`
  • @un-lucky:- 我想在我的项目中显示 ₹ 符号。我已经在上面的example 中解释了这段代码。如果您运行此代码,它会将 html 名称转换为它们的符号(例如:€)。对于€它的工作正常,但对于₹它不起作用。
  • @ib11:- 我用完整的代码更新了我的问题。请拿出来。
  • @Sanjiv 符号已经在输入中?

标签: c# html


【解决方案1】:

首先,我认为这里有一个更基本的问题。

您的input 字符串不包含字符串"&amp;euro;"

如果你把字典改成这样:

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

然后你会看到输出其实是:

&amp;#8377; 5000 &amp;#8364;

所以你没有看到你认为你看到的,因为虽然"₹" 是字符串的一部分,但"&amp;euro;" 不是。


也就是说,阅读此内容后,似乎并非所有浏览器都支持此卢比符号的 html 实体代码。

如果您想要完成以下代码,则使用 unicode 代码:

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();
        }
    }
}

另外,请检查此问题和接受的答案,我认为这将为您提供信息:

Displaying the Indian currency symbol on a website

最后,如果您想在 html 中正确表示符号,可以使用以下代码:

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

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

HTH

【讨论】:

    【解决方案2】:

    您是否尝试过设置控制台输出编码?

    例如

    Console.OutputEncoding = System.Text.Encoding.UTF8;
    

    【讨论】:

    • :- 实际上我已经为其他符号(!、@、#、$、%、^、&、* 等)实现了我的代码,它适用于所有符号,唯一的问题是₹ 符号。如果可能的话,请告诉上面代码的一些解决方案。
    • 我的输出是:-> ₹ 5000。而不是 ₹ 符号,它打印它的 html 代码(即:- ₹)
    • 在您的替换字典中,您正在替换“€”而不是欧元。这是为什么呢?
    • 因为 € 有 &euro html 名称,但对于₹我没有找到任何 html 名称。所以我直接使用了₹符号。
    • string.replace 不会替换 html 名称。因此上面的代码将输出输入中的欧元符号,而不是进行任何替换。
    猜你喜欢
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多