Named Formats!
原文发布时间为: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!的更多相关文章
- NetAdvantage 笔记
1.UltraControlBase Class Members 1.BeginUpdate Method Sets the IsUpdating flag to true which prevent ...
- FreeMarker-Built-ins for numbers
http://freemarker.org/docs/ref_builtins_number.html#topic.extendedJavaDecimalFormat Page Contents ab ...
- [转] 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/ ...
- table2excel使用
原table2excel代码 /* * 采用jquery模板插件——jQuery Boilerplate * * Made by QuJun * 2017/01/10 */ //table2excel ...
- 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 ...
- InnoDB的Named File Formats
随着InnoDB存储引擎的发展,新的页数据结构有时用来支持新的功能特性.比如前面提到的InnoDB Plugin,提供了新的页数据结构来支持表压缩功能,完全溢出的(Off page)大变长字符类型字段 ...
- Django 运行报错 ImportError: No module named 'PIL'
importError: No module named pil WIN7 64位系统安装 Python PIL 首先通过easy_install安装 说找不到pil模块. 第二通过去官网找:http ...
- Collection of Boot Sector Formats for ISO 9660 Images
http://bazaar.launchpad.net/~libburnia-team/libisofs/scdbackup/view/head:/doc/boot_sectors.txt Colle ...
- ImportError: No module named 'requests'
补充说明: 当前环境是在windows环境下 python版本是:python 3.4. 刚开始学习python,一边看书一边论坛里阅读感兴趣的代码, http://www.oschina.net/c ...
随机推荐
- 二十四、MySQL ALTER命令
MySQL ALTER命令 当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本章教程前让我们先创建一张表,表名为:testalter_tbl. root@ho ...
- 【Python学习之十】yield之send方法
yield作用 简单地讲,yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator.下面以斐波拉 ...
- C语言:自己编写的简易ftp客户端,包含(列表,进入目录,上传文件,下载文件,删除文件)功能
//简易ftp客户端#include <stdio.h> #include <string.h> #include <sys/types.h> #include & ...
- tcl之list操作-llength/lindex/lrange/linsert/lreplace
- Kali 中文家目录改英文目录
中文版Kali装好之后,家目录会中文显示,不便操作 root@kali:~# ls -l drwxr-xr-x root root .0K 7月 : 公共 drwxr-xr-x root root . ...
- Python中函数参数类型和参数绑定
参数类型 Python函数的参数类型一共有五种,分别是: POSITIONAL_OR_KEYWORD(位置参数或关键字参数) VAR_POSITIONAL(可变参数) KEYWORD_ONLY(关键字 ...
- 浅谈I/O模型
在学习线程,NIO等知识时都需要知道一些基础知识. 一.什么是同步或异步 同步:个人通俗理解多个人排队打饭一个窗口,只有前面一个人打完了,后面的人才能打.如果前面人因为什么原因一直站在那里不走,后面的 ...
- loj6392 「THUPC2018」密码学第三次小作业 / Rsa
还是挺好做的,\((e_1,e_2)=1 \Rightarrow e_1s+e_2t=0\),\(m \equiv m^1 \equiv m^{e_1s+e_2t} \equiv c_1^s c_2^ ...
- linux学习(三) -- lnmp环境切换php版本,并安装相应redis扩展
原创文章,转载请注明出处 我想配置的环境是ubuntu+nginx+mysql+php+redis,其中php装两个版本,php7和php56 ubuntu+nginx+mysql+php的环境配 ...
- 测试环境docker化—容器集群编排实践
本文来自网易云社区 作者:孙婷婷 背景 在前文<测试环境docker化-基于ndp部署模式的docker基础镜像制作>中已经详述了docker镜像制作及模块部署的过程,按照上述做法已可以搭 ...