因為習慣了C#的 String.Format 去組合字串,最近在寫ASP與JavaScript時沒有這個功能實在是有夠不方便。

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

JavaScript:

ASP與JavaScript的 String.Format 方法String.format = function()

來源Blog

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

ASP
ASP與JavaScript的 String.Format 方法 Function StringFormat(Source,Values,IsSQL)
ASP與JavaScript的 String.Format 方法        
If Source = null Or Not IsArray(Values) Then        
ASP與JavaScript的 String.Format 方法            StringFormat 
null
ASP與JavaScript的 String.Format 方法        
End If        
ASP與JavaScript的 String.Format 方法        
ASP與JavaScript的 String.Format 方法        
For i = 0 To UBound(Values)
ASP與JavaScript的 String.Format 方法            
If  IsSQL Then
ASP與JavaScript的 String.Format 方法              Source 
= Replace(Source , "{" & i & "}",Replace(Values(i),"'","''"))
ASP與JavaScript的 String.Format 方法            
Else
ASP與JavaScript的 String.Format 方法              Source 
= Replace(Source , "{" & i & "}",Values(i))
ASP與JavaScript的 String.Format 方法            
End IF            
ASP與JavaScript的 String.Format 方法        
Next  
ASP與JavaScript的 String.Format 方法        StringFormat 
= Source             
ASP與JavaScript的 String.Format 方法   
End Function

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

使用方法 : StringFormat("Select * Form Account Where ID='{0}' AND PW='{1}'" , Array(ID,PW),true)

相关文章:

  • 2021-11-03
  • 2021-10-13
  • 2021-07-07
  • 2021-09-18
  • 2022-12-23
  • 2021-06-10
  • 2021-06-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
相关资源
相似解决方案