码迷,mamicode.com
首页 > Windows程序 > 详细

c#和JS字符串format实现(可实现JSON对象,c#实体对象,C#匿名对象,替换)

时间:2019-08-06 15:40:28      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:new   cti   key   java   自己   调用   ref   ret   sum   

  • 首先看一下JS实现方法,此方法在网上找的,具体地址忘了,代码如下:
  • String.prototype.format= function () {
            if (arguments.length == 0) return this;
            var param = arguments[0];
            var s = this;
            if (typeof (param) == ‘object‘) {
                for (var key in param)
                    s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
                return s;
            } else {
                for (var i = 0; i < arguments.length; i++)
                    s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
                return s;
            }
        }

    javascript调用:

    var str = "js实现用{two}自符串替换占位符{two} {three}  {one} ".format({one: "I",two: "LOVE",three: "YOU"});
    var str2 = "js实现用{1}自符串替换占位符{1} {2}  {0} ".format("I","LOVE","YOU");

    2.C#后台实现代码,下面方法自己写的;

            /// <summary>
            /// 字符串格式占位替换
            /// </summary>
            /// <param name="str">字符串</param>
            /// <param name="obj">替换对象(T,匿名对象,Newtonsoft.Json.Linq.JObject)</param>
            /// <returns></returns>
            public static string Format(string str, object obj)
            {
                if (str.Length == 0)
                {
                    return str;
                }
                string s = str;
                if (obj.GetType().Name == "JObject")
                {
                    foreach (var item in (Newtonsoft.Json.Linq.JObject)obj)
                    {
                        var k = item.Key.ToString();
                        var v = item.Value.ToString();
                        s = Regex.Replace(s, "\\{" + k + "\\}", v, RegexOptions.IgnoreCase);
                    }
                }
                else
                {
                    foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties())
                    {
                        var xx = p.Name;
                        var yy = p.GetValue(obj).ToString();
                        s = Regex.Replace(s, "\\{" + xx + "\\}", yy, RegexOptions.IgnoreCase);
                    }
                }
                return s;
            }

    c#调用方法:

     string ssew = "{one:‘I‘,two: ‘LOVE‘,three: ‘YOU‘}";
                Newtonsoft.Json.Linq.JObject o2 = Newtonsoft.Json.Linq.JObject.Parse(ssew);
                string sew = CommonFunc.Format("{ONE} {TWO} {three}  ", o2);
    
                var ste = new { one = "I", two = "LOVE", three = "You" };
                string ese = CommonFunc.Format("{ONE} {TWO} {three}  ", ste);

    c#和JS字符串format实现(可实现JSON对象,c#实体对象,C#匿名对象,替换)

    标签:new   cti   key   java   自己   调用   ref   ret   sum   

    原文地址:https://blog.51cto.com/317057112/2427053

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!