原文发布时间为:2011-06-26 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Text;
using System.Web;
using System.Web.UI;

namespace StringLib
{
public static class HenriFormatter
{
public static string FormatWith(this string format, object source)
{
if (format == null)
throw new ArgumentNullException("format");

var result = new StringBuilder(format.Length * 2);
var expression = new StringBuilder();

var e = format.GetEnumerator();
while (e.MoveNext())
{
var ch = e.Current;
if (ch == '{')
{
while (true)
{
if (!e.MoveNext())
throw new FormatException();

ch = e.Current;
if (ch == '}')
{
result.Append(OutExpression(source, expression.ToString()));
expression.Length = 0;
break;
}
if (ch == '{')
{
result.Append(ch);
break;
}
expression.Append(ch);
}
}
else if (ch == '}')
{
if (!e.MoveNext() || e.Current != '}')
throw new FormatException();
result.Append('}');
}
else
{
result.Append(ch);
}
}

return result.ToString();
}

private static string OutExpression(object source, string expression)
{
string format = "{0}";

int colonIndex = expression.IndexOf(':');
if (colonIndex > 0)
{
format = "{0:" + expression.Substring(colonIndex + 1) + "}";
expression = expression.Substring(0, colonIndex);
}

try
{
return DataBinder.Eval(source, expression, format) ?? string.Empty;
}
catch (HttpException)
{
throw new FormatException();
}
}
}

}

===============

MembershipUser user = Membership.GetUser();

"{UserName} last logged in at {LastLoginDate}".FormatWith(user);

output===》 njwu last logged in at 2010年1月6日08:07:39

=============

"{CurrentTime} - {UrL}".FormatWith(new { CurrentTime = DateTime.Now, url = "http://hi.baidu.com/handboy" });

output==》2010年1月6日08:07:39 - http://hi.baidu.com/handboy

===================

"{{{UserName}}} last logged in at {LastLoginDate}".FormatWith(user);

output==> {njwu} last logged in at 2010年1月6日08:07:39

================

Console.WriteLine("{date:yyyy-MM-dd},{url}".FormatModel(new { date = DateTime.Now, url = "http://hi.baidu.com/handboy" }));

output==> 2011-06-26,http://hi.baidu.com/handboy

==================

Named Formats!的更多相关文章

  1. NetAdvantage 笔记

    1.UltraControlBase Class Members 1.BeginUpdate Method Sets the IsUpdating flag to true which prevent ...

  2. FreeMarker-Built-ins for numbers

    http://freemarker.org/docs/ref_builtins_number.html#topic.extendedJavaDecimalFormat Page Contents ab ...

  3. [转] JSON Web Token in ASP.NET Web API 2 using Owin

    本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...

  4. table2excel使用

    原table2excel代码 /* * 采用jquery模板插件——jQuery Boilerplate * * Made by QuJun * 2017/01/10 */ //table2excel ...

  5. JSON Web Token in ASP.NET Web API 2 using Owin

    In the previous post Decouple OWIN Authorization Server from Resource Server we saw how we can separ ...

  6. InnoDB的Named File Formats

    随着InnoDB存储引擎的发展,新的页数据结构有时用来支持新的功能特性.比如前面提到的InnoDB Plugin,提供了新的页数据结构来支持表压缩功能,完全溢出的(Off page)大变长字符类型字段 ...

  7. Django 运行报错 ImportError: No module named 'PIL'

    importError: No module named pil WIN7 64位系统安装 Python PIL 首先通过easy_install安装 说找不到pil模块. 第二通过去官网找:http ...

  8. Collection of Boot Sector Formats for ISO 9660 Images

    http://bazaar.launchpad.net/~libburnia-team/libisofs/scdbackup/view/head:/doc/boot_sectors.txt Colle ...

  9. ImportError: No module named 'requests'

    补充说明: 当前环境是在windows环境下 python版本是:python 3.4. 刚开始学习python,一边看书一边论坛里阅读感兴趣的代码, http://www.oschina.net/c ...

随机推荐

  1. Uva 填充正方形

    暴力出奇迹 #include<iostream> #include<cstdio> using namespace std; +; int T,n; char S[maxn][ ...

  2. Mybatis中的增删改查

    相比jdbc mybatis在操作数据库方面比jdbc节省了大量的代码,及大量的代码冗余.使得操作起来更加简洁. 在Mapper中分别有着 select,insert, update,delete的这 ...

  3. hprose 1.0(rpc 框架) - 关于跨域和P3P的声明

    private function sendHeader($context) { if ($this->onSendHeader !== null) { $sendHeader = $this-& ...

  4. Django基于类的增删改查,简单逻辑都不用写

    Django是Python中一个非常牛逼的web框架,他帮我们做了很多事,里边也提前封装了很多牛逼的功能,用起来简直不要太爽,在写网站的过程中,增删改查这几个基本的功能我们是经常会用到,Django把 ...

  5. 适合学习C语言开源项目——嵌入式脚本语言 Berry

    嵌入式脚本语言 Berry github网址 :https://github.com/Skiars/berry Berry 是一款面向小型嵌入式系统的脚本语言,目前发布了 0.1.0 版本.相比于其他 ...

  6. 【STM32】IIC的基本原理(实例:普通IO口模拟IIC时序读取24C02)(转载)

     版权声明:本文为博主原创文章,允许转载,但希望标注转载来源. https://blog.csdn.net/qq_38410730/article/details/80312357 IIC的基本介绍 ...

  7. Ubuntu下的定时备份数据库

    1.编写备份数据库的shell脚本 mysqldump -uUserName -pPassword dbName >/XXX/XXXX/XXXX/fileName_$(date +%Y%m%d_ ...

  8. OpenCV学习笔记(一) 环境配置

    Visual Studio 2010 VS2010对应的OpenCV的lib文件(build\x86\vc10\lib)分为debug模式和release模式两种:debug模式牺牲速度,但能提供更多 ...

  9. 线程、进程、队列、IO多路模型

    操作系统工作原理介绍.线程.进程演化史.特点.区别.互斥锁.信号.事件.join.GIL.进程间通信.管道.队列.生产者消息者模型.异步模型.IO多路复用模型.select\poll\epoll 高性 ...

  10. NOS直传加速服务

    本文来自网易云社区 作者:孙建良 最近团队在对存储系统做一些性能测试,期间遇到了不少问题,测试过程中得出的结果也没有很好的数据支撑,所以尝试了非常多的方法来对性能问题进行定位. 小王童鞋是挺厉害的,使 ...