1、JavaScript

function stringformat() {var args = Array.prototype.slice.call(arguments);if (args.length == 0) { return '';}     if (args.length == 1) { return args[0]; }     var str = args.shift();return str.replace(/\\?\{([^{}]+)\}/g,function(match, name) {if (match.charAt(0) === '\\') {return match.slice(1);}return (args[name] === undefined) ? match : args[name];});
}// 返回 my name is Rain Man and age is 28
stringformat('my name is {0} and age is {1}', 'Rain Man', 28, 'an');

2、PHP

function string_format() {       $args = func_get_args();     if (count($args) == 0) { return '';}     if (count($args) == 1) { return $args[0]; }     $str = array_shift($args);$GLOBALS['OBJ'] = $args;$str = preg_replace_callback('/\\\?{([^{}]+)}/', function ($matches) {list($matche, $name) = $matches;if ($matche[0] === '\\') {return substr($matche, 1);}$obj = $GLOBALS['OBJ'];return isset($obj[$name]) ? $obj[$name] : $matche;},$str); $GLOBALS['OBJ'] = NULL;return $str;
}// my name is Rain Man and age is 28
echo string_format('my name is {0} and age is {1}', 'Rain Man', 28, 'an');

JavaScript PHP模仿C#中string.format效果相关推荐

  1. java %tb_Java中String.format()的使用指北 ~ 王诛魔

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象,format()方法有两种重载形式. format(String format, Object- a ...

  2. Unity3D C#中 String.Format的格式限定符

    Format方法将多个对象格式化成一个字符串Format方法解析格式字符串的原理: (1).格式字符串中的{0}会被替换成格式字符串之后的第一个参数,以此类推 (2).Format方法解析格式字符串时 ...

  3. 总结了C#中string.format用法。分享给大家供大家参考。具体分析如下:

    String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. Str ...

  4. Objective-C 字符串拼接函数 多个不同类型的参数拼接到一个字符串 类似于Java中 String.format()方法的原生API

    总目录 iOS开发笔记目录 从一无所知到入门 文章目录 需求 Screenshot Code Output 需求 我有多个参数(类型也许不同),需要拼接到一个字符串中. 在Java中有String.f ...

  5. C#中string.format用法详解 格式字符串

    string.Format对C#字符串格式化 String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 ...

  6. C#中string.format 格式转换用法详解

    String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. Str ...

  7. java object... args_java中String.format(format,args)中的args是动态的,如何定义format?

    来自intellij format(String format, Object... args) public static String format(String var0, Object... ...

  8. String.Format in javascript

    有些时候,我们确实需要在JavaScript中进行字符串替换,类似于C#中的String.Format()方法一样,只不过这种格式化替换只局限于对由'{0}','{1}','{2}'...所组成的&q ...

  9. Java中利用MessageFormat对象实现类似C# string.Format方法格式化

    我们在写C#代码的时候常常会使用到string.Format("待格式化字符串{0},{1},....",参数1,参数2,...),来格式化字符串,特别是拼接字符的时候,这种方式使 ...

最新文章

  1. 在公共区块链中通过加密保护数据
  2. U-Boot移植教程之一:U-Boot分析与启动过程
  3. 如何使用 controllerExtensions 给 SAP Fiori Elements List Report 的表格注册事件响应函数
  4. Bloomfilter 的应用场景
  5. Python开发利器PyCharm 2.7附注册码
  6. 9-Springboot任务管理
  7. csdn博客图片复制不过来怎么办?如何转载?
  8. Nuget如何管理本地的包
  9. cefsharp作为采集工具(学习笔记)
  10. 微信中文件为什么不能另存?
  11. Java 映射(Map)和集合(Set) 用法总结
  12. 2019111 控制台上实现极乐净土(有图有背景音乐)
  13. mingw不能安装_C语言/C++初学——Visual Studio环境的安装配置与使用
  14. word,wps设置页眉和页码
  15. 初学单片机,该如何选择单片机开发板?
  16. 2022最新WiFi大师小程序V3.0.9独立版源码
  17. thinkPHP6报错:Failed to listen on 0.0.0.0:8000 (reason: һַȨ޲“
  18. python用cartopy包画地图_python绘制地图的利器Cartopy使用说明
  19. 铁氧体磁芯电感的特性大揭秘
  20. EtherCAT的从站配置

热门文章

  1. python就业前景分析-最新的Python就业前景分析一览表
  2. python100个免费实例-Python实例100个(基于最新Python3.7版本)
  3. python白名单验证-python脚本简单检测ip合法性并添加到白名单文件
  4. python语言教程-Python语言教程手册
  5. 简明python教程购买-简明python教程哪版(python看什么书)
  6. 请务必注意 Redis 安全配置,否则将导致轻松被入侵
  7. Qt中打开excel文件
  8. windows下netstat的使用
  9. 【NOIP1999】导弹拦截
  10. Python学习之==json处理