c# - 这个 VBScript 函数在做什么

标签 c# vbscript

我在 VBScript 中有一个函数,它在做什么?如何使用 C# 2.0 简化它。

Function FormatString(format, args)
    Dim RegExp, result

    result = format

    Set RegExp = New RegExp 

    With RegExp
        .Pattern = "\{(\d{1,2})\}"
        .IgnoreCase = False
        .Global = True
    End With

    Set matches = RegExp.Execute(result)

    For Each match In matches   
        dim index
        index = CInt(Mid(match.Value, 2, Len(match.Value) - 2))
        result = Replace(result, match.Value, args(index))
    Next
    Set matches = nothing
    Set RegExp = nothing

    FormatString = result
End Function

谢谢!

最佳答案

看起来像 .NET 的简化版本 String.Format方法。

它采用带有花括号分隔的占位符(例如 "{0} {1}")的格式字符串,并依次用 args< 中的相应值替换每个占位符 数组。您可以将它换成 String.Format,而不会改变任何功能。

关于c# - 这个 VBScript 函数在做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7925075/

相关文章:

c# - 使用 .NET 的 ANSI 着色控制台输出

c# - 在 C# 准备语句中执行多个 SQL 查询

c# - "Hiding"系统光标

mysql - 在虚拟化 Windows 2003 x64 服务器上使用 ASP 连接到 MySQL 数据库

vbscript - ReDim Array语法完整性检查-预期语句错误

c# - 如何获取 2 个范围之间的重叠天数?

sql-server - DTS 转换为 SSIS 派生列表达式

php - 调度程序不运行 0x8004131F

vba - VBScript - 如何让程序等待进程完成?

c# - View 模型中的异步方法 : should this return Task or void?