惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
L
LangChain Blog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
D
Docker
WordPress大学
WordPress大学
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
博客园 - 叶小钗
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog
博客园 - Franky

博客园 - 空空儿

硬盘分区、寻址和系统启动过程 asp.net异步页 获取SQL Server数据行的物理地址信息(%%lockress%% & %%physloc%%) 利用Spire.DataExport将数据(Database/DataTable)导成各种文件 使用XML的value()方法将多行数据合并成一列 获取所有的外键信息 ASP.NET application and page life cycle SQL Server 2005 CLR 调用Web Service需要注意的几个问题 SQL SERVRE 2005 CLR TVF错误:从用户定义的表值函数获取新行时出错:Data access is not allowed in this context. PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 在C#里使用属性 - 空空儿 - 博客园 Read text file (txt, csv, log, tab, fixed length) 批量写数据---将XML数据批量写入数据库 对对象类型和调用方法属性进行存储以提升反射性能 过程 sp_addlinkedsrvlogin,第 91 行解密过程中出错的解决办法 生成大量随机字符串不同实现方式的效率对比 用SQL SERVER 2005新提供的命令实现行列转换 APM (异步编程模型) 利用宏让ERStudio生成代码文件
JavaScript里String.Format方法的实现
空空儿 · 2011-06-26 · via 博客园 - 空空儿

在Javascript里通过原型扩展和正则表达式实现类似于C#里的String.Format方法.

方法实现:

String.prototype.format = function (args) {
       
var str = this;
       
return str.replace(new RegExp("{-?[0-9]+}""g"), function(item) {
       
var intVal = parseInt(item.substring(1, item.length - 1));
       
var replace;
       
if (intVal >= 0) {
            replace 
= args[intVal];
       } 
else if (intVal === -1) {
            replace 
= "{";
       } 
else if (intVal === -2) {
            replace 
= "}";
       } 
else {
            replace 
= "";
       }
       
return replace;
    });
};

 使用如下:

var str = "She {1} {0}{2} by the {0}{3}. {-1}^_^{-2}";
str 
= str.format(["sea""sells""shells""shore"]);
alert(str);
//out put: She sells seashells by the seashore. {^_^}

原文地址:String.Format in JavaScript