原文发布时间为: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. P4747 D’s problem(d)

    时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试题 描述 题目描述 小D是一名魔法师,它最喜欢干的事就是对批判记者了. 这次记者招待会上,记者对 ...

  2. MySQL - FIND_IN_SET 函数使用方法

    SELECT * FROM xxxTableName x WHERE FIND_IN_SET(x.id, '1,2,3,4,5,6,7,8');   如上查询,意为:xxxTableName 表中 x ...

  3. Python中字符串String的基本内置函数与过滤字符模块函数的基本用法

    Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...

  4. day 52 Django基础一之web框架的本质

    Django基础一之web框架的本质   django第一天 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于D ...

  5. Green Space【绿色空间】

    Green Space Living in an urban area with green spaces has a long-lasting positive impact on people's ...

  6. n个人排队都不站在原来的位置

    一.题目描述 有n个人首先站成一排,请问,当n个人第二次再重新排列,每个人都不在原来的位置上,问有多少种站法.例如,原来有3个人,ABC,那么第二次每个人都不在原来的位置上有2种站法,BCA和CAB, ...

  7. adb offline解决办法

    假如你连接手机之后,adb devices找不到设备,或者找到了设备,但是device ID后总是offline的状态,那估计就是驱动有问题. 强烈建议1.安装豌豆荚,它可以自己主动修复手机驱动,一般 ...

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

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

  9. JSP自定义tag控件标签

    JSP支持自定tag的方法,那就是直接讲JSP代码保存成*.tag或者*.tagx的标签定义文件.tag和tagx文件不仅支持经典jsp代码,各种标签模版代码,还支持xml样式的jsp指令代码. 按照 ...

  10. 设计模式之第21章-状态模式(Java实现)

    设计模式之第21章-状态模式(Java实现) “what are you 干啥了?怎么这么萎靡不振?”“昨晚又是补新番,又是补小笼包,睡得有点晚啊.话说杨过的那个雕兄真是太好了,每天给找蛇胆,又陪练武 ...