文章分类

站点统计

  • 分类总数: 13 个
  • 文章总数: 145 篇
  • 评论总数: 48 条
  • 附件总数: 59 个
  • 建站日期: 2008-08-18
  • 访问总数: 491584 人次
  • RSS订阅: 文章|评论

ASP中实现C#的String.Format函数与C语言的printf函数

Admin 于 2008-12-03 14:17:20 发表其它

①、StringFormat

代码:

  1. Function StringFormat(sVal, aArgs) 
  2.  Dim i 
  3.  For i=0 To UBound(aArgs) 
  4.   sVal = Replace(sVal,"{" & CStr(i) & "}",aArgs(i)) 
  5.  Next 
  6.  StringFormat = sVal 
  7. End Function 

使用方法:

  1. StringFormat("this {0} a tes{1} string containing {2} values", Array("is","t","some")) 

②、printf函数

http://www.codeproject.com/KB/vbscript/fmt.aspx

  1. '============================================================================ 
  2. ' takes a string with format characters and an array 
  3. ' to expand. 
  4. ' 
  5. ' the format characters are always "%x", independ of the 
  6. ' type. 
  7. ' 
  8. ' Use double percent (%%) to denote an actual percent 
  9. ' 
  10. ' usage example: 
  11. ' dim str 
  12. ' str = fmt( "hello, Mr. %x, today's date is %x.", Array("Miller",Date) ) 
  13. ' response.Write str 
  14. ' 
  15. ' additionally the support was added to allow for fixed field widths 
  16. ' adding a number between the % and x will set the field width to at least that size 
  17. ' example: 
  18. ' str = fmt( "%10x %10x %10x", Array("Jason", "Steve", "Smith") ) 
  19. ' response.write str 
  20. ' output: 
  21. ' 'Jason Steve Smith ' 
  22. '============================================================================ 
  23. Function fmt(ByVal str, ByVal args) 
  24.     Dim res ' the result string. 
  25.     res = "" 
  26.  
  27.     Dim oRE 'the RegEx 
  28.     oRE = New regexp 
  29.     Dim fieldLen ' length of the field 
  30.     Dim strOffset 
  31.     Dim argLen ' length of array string 
  32.     Dim strSearchOn ' string to perform RegEx on 
  33.     strSearchOn = "" 
  34.  
  35.     Dim pos ' the current position in the args array. 
  36.     pos = 0 
  37.  
  38.     Dim i 
  39.     For i = 1 To Len(str) 
  40.         ' found a fmt char. 
  41.         If Mid(str, i, 1) = "%" Then 
  42.             'make sure we are not at the end of the string 
  43.             If i Then 
  44.                 ' normal percent. 
  45.                 If Mid(str, i + 1, 1) = "%" Then 
  46.                     res = res & "%" 
  47.                     i = i + 1 
  48.                 Else 
  49.                     ' determine if there is a field length 
  50.                     oRE.Pattern = "%(\d*)x.*{Article.Explain}quot; 
  51.                     oRE.Global = False 
  52.                     oRE.IgnoreCase = False 
  53.                     strSearchOn = Mid(str, i) ' the rest of the string 
  54.                     fieldLen = oRE.Replace(strSearchOn, "$1"
  55.                     If fieldLen = "" Then 
  56.                         ' no length specified, expand from array. 
  57.                         res = res & CStr(args(pos)) 
  58.                         strOffset = 0 
  59.                     Else 
  60.                         ' field is specified, expand from array then pad 
  61.                         res = res & CStr(args(pos)) 
  62.                         argLen = Len(CStr(args(pos))) ' this is how large the token was 
  63.                         If argLen < fieldLen Then ' determine if we need more padding 
  64.                             res = res & Space(fieldLen - argLen) ' add the padding to the string 
  65.                         End If 
  66.                         strOffset = Len(fieldLen) 
  67.                     End If 
  68.  
  69.                     pos = pos + 1 
  70.                     i = i + strOffset + 1 
  71.                 End If 
  72.             End If 
  73.  
  74.             ' found a normal char. 
  75.         Else 
  76.             res = res & Mid(str, i, 1) 
  77.         End If 
  78.     Next 
  79.  
  80.     fmt = res 
  81.  
  82.     ' clean-up 
  83.     oRE = Nothing 
  84. End Function 

 

被阅954次, 0票ASP 发表评论
1 / 1 / 1 | « 1 » |
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号