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 ...
随机推荐
- CentOS---zookeeper安装(单机、伪集群、集群)
一:单机安装: 可以参考下面的伪集群安装方式 不同点: 不需要在data目录下创建 myid 文件 不需要配置集群 配置好后的启动和状态查询命令相同!! 二:伪集群模式 伪集群模式就是在同一主机上启动 ...
- windows_Bat_Scripts查看系统IP-更改regedit-更新系统补丁
1.1 脚本名称 Update_patch.bat 1.2 脚本代码 @echo off :menu cls mode con cols=48 lines=27 & color 0 ...
- eclipse中使用git上传项目
eclipse中使用git上传项目 先需要上传到本地仓库 先找到此选项打钩 再如下 再添加的属性则可以自动填充邮箱和密码 之后 右键选择import 点击找到git 选中 下一步 如果没有找到git ...
- JZOJ 5809. 【NOIP2008模拟】数羊
5809. [NOIP2008模拟]数羊 (File IO): input:sheep.in output:sheep.out Time Limits: 1000 ms Memory Limits: ...
- oop 单例模式
- 在windows和Linux下安装nodejs
在windows下安装nodejs 1.首先下载nodejs安装包, https://nodejs.org/en/download/ 点击下载相应的版本 然后将文件夹解压到安装目录(任意,不做规定) ...
- POJ 3414 BFS 输出过程
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17456 Accepted: 7407 Special J ...
- python之时间处理time模块
import time import datetime ''' print(time.time()) #返回当前系统时间戳 print(time.ctime()) #返回当前系统时间 print(ti ...
- Kali 安装VMtools(最新)
老方法安装的VMtools不能进行主宿切换,下面是kali最新版安装VMtools的方法 一.换国内源&更新源 参考 Kali 2017更新源 二.安装VMtools apt-get inst ...
- 4819: [Sdoi2017]新生舞会(分数规划)
4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1031 Solved: 530[Submit][Statu ...