您的位置:首页 > Web前端 > JavaScript

ASP與JavaScript的 String.Format 方法

2007-08-24 09:27 281 查看
因為習慣了C#的 String.Format 去組合字串,最近在寫ASP與JavaScript時沒有這個功能實在是有夠不方便。

不過後來發現有人貼JavaScript方法,就以此方法改成ASP的

JavaScript:

String.format = function()

來源Blog

使用方式 : String.format('Hello. My name is {0} {1}.', firstName, lastName);

ASP

Function StringFormat(Source,Values,IsSQL)

If Source = null Or Not IsArray(Values) Then

StringFormat null

End If

For i = 0 To UBound(Values)

If IsSQL Then

Source = Replace(Source , "{" & i & "}",Replace(Values(i),"'","''"))

Else

Source = Replace(Source , "{" & i & "}",Values(i))

End IF

Next

StringFormat = Source

End Function

我有加上 是不是SQL 語法 替換 ' 成 二個 ' ,防止 SQL injection

使用方法 : StringFormat("Select * Form Account Where ID='{0}' AND PW='{1}'" , Array(ID,PW),true)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: