字符串替换问题

Alps_lou 2009-01-30 12:08:52
请问在VB中如何实现如同VC中如下字符串格式化功能:

strUserName = "Test user";
iNumber = 66
str.format("The user %s ID number is %d",strUserName,iNumber)

最终得到的结果为:
The user Test user ID number is 66
...全文
344 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
PctGL 2009-02-03
  • 打赏
  • 举报
回复

直接调用 wsprintf 就行了啊

wsprintf 缓冲,原字符串,需要格式化的数据
东方之珠 2009-02-02
  • 打赏
  • 举报
回复
收而不藏---共享!
sourcceinsigt35 2009-02-02
  • 打赏
  • 举报
回复
不懂,帮顶
netsocket 2009-02-02
  • 打赏
  • 举报
回复
不懂,帮顶
xrongzhen 2009-02-01
  • 打赏
  • 举报
回复
mark
嗷嗷叫的老马 2009-02-01
  • 打赏
  • 举报
回复
收藏.
bdzwj 2009-01-31
  • 打赏
  • 举报
回复


'-----------------------------------------------------------------------------------
' 函数名称: wsprintf()
' 函数功能: 仿C++中sprintf()函数格式化输出数据
'
' 参数说明:
' [In] strFMT : String '输出内容和输出格式化因子的字符串
' [In] vArgs() : Variant '输出的参数列表
'
' 返回类型: 字符型, 返回格式化后的字符串
'
' 格式化因子:
' %s ---- 字符串
' %d ---- 日期型
' %i ---- 数字型
' %x ---- 十六进制数字
'-----------------------------------------------------------------------------------
Public Function wsprintf(ByVal strFMT As String, ParamArray vArgs() As Variant) As String
Dim strArr() As String
Dim strRight As String
Dim strOut As String

Dim i As Long, j As Long
Dim lCount As Long
Dim Args() As Variant

strArr = Split(strFMT, "%")

On Error Resume Next
Args = IIf(IsArray(vArgs(0)), vArgs(0), vArgs)

lCount = UBound(strArr)
For i = 1 To lCount
If Right$(strArr(i - 1), 1) = "\" Then
strArr(i - 1) = Left$(strArr(i - 1), Len(strArr(i - 1)) - 1)
strArr(i) = "%" & strArr(i)
ElseIf strArr(i) <> vbNullString Then
strRight = Right$(strArr(i), Len(strArr(i)) - 1)
Select Case LCase$(Left$(strArr(i), 1))
Case "x"
strArr(i) = "&H" & Hex$(Args(j)) & strRight
Case "d"
strArr(i) = Format(Args(j), "yyyy-MM-dd hh:mm:ss") & strRight
Case Else
strArr(i) = Args(j) & strRight
End Select
j = j + 1
End If
Next
strOut = Join(strArr, "")
strOut = Replace(strOut, "\n", vbCrLf)
wsprintf = strOut
End Function


嗷嗷叫的老马 2009-01-31
  • 打赏
  • 举报
回复
Function fmt(str, args)
' works like the printf-function in C.
' takes a string with format characters and an array
' to expand.
'
' the format characters are always "%x", independ of the
' type.
'
' usage example:
' dim str
' str = fmt( "hello, Mr. %x, today's date is %x.", Array("Miller",Date) )
' response.Write str
Dim res ' the result string.
res = ""

Dim pos ' the current position in the args array.
pos = 0

Dim i
For i = 1 To Len(str)
' found a fmt char.
If Mid(str, i, 1) = "%" Then
If i < Len(str) Then
' normal percent.
If Mid(str, i + 1, 1) = "%" Then
res = res & "%"
i = i + 1

' expand from array.
ElseIf Mid(str, i + 1, 1) = "x" Then
res = res & CStr(args(pos))
pos = pos + 1
i = i + 1
End If
End If

' found a normal char.
Else
res = res & Mid(str, i, 1)
End If
Next

fmt = res
End Function

以前收藏的一个例子
bdzwj 2009-01-30
  • 打赏
  • 举报
回复
str = "The user " & strUserName & "ID number is " & iNumber
debug.print str
feiyun0112 2009-01-30
  • 打赏
  • 举报
回复
str=("The user @user ID number is @id",
str=replace(str,"@user",strusername)
str=replace(str,"@id",inumber)

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

http://feiyun0112.cnblogs.com/
Alps_lou 2009-01-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdzwj 的回复:]
str = "The user " & strUserName & "ID number is " & iNumber
debug.print str
[/Quote]
谢谢回复,我想将字符串"The user %s ID number is %d"放到资源中,然后,读取出来,进行替换。
所以楼上的方法就不行了。

1,065

社区成员

发帖
与我相关
我的任务
社区描述
VB 资源
社区管理员
  • 资源
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧