VB如何用百分号格式化字符(像%s %d的)
C语言:printf("整数:%d 浮点数:%f 字符串:%s", 64, 3.1415926, "abc");
Python:
print("整数:%d 浮点数:%f 字符串:%s" % ( 64, 3.1415926, "abc" ))
VB怎么实现用百分号格式字符
简单做了个格式化字符串解析,供参考 Private Sub Command1_Click() s = printf("%ttt\n%s\t\t%f %d\n", "aa", 3.1415926, 999) Debug.Print s End Sub Private Function printf(formatStr$, ParamArray Parameters() As Variant) Dim Count%, index%, vari%, slen%, s$ index = InStr(formatStr, "%") If index = 0 Then printf = formatStr Exit Function End If printf = Mid(formatStr, 1, index - 1) slen = Len(formatStr) vari = 0 For i = index To slen s = Mid(formatStr, i, 1) If s = "%" And i + 1 < slen Then i = i + 1 s = Mid(formatStr, i, 1) Select Case s Case "%" s = "%" Case "s" s = Parameters(vari) vari = vari + 1 Case "f" s = Format(Parameters(vari)) vari = vari + 1 Case "d" s = Format(Parameters(vari)) vari = vari + 1 Case Else '%后面的不识别则直接输出 s = "%" & s End Select End If printf = printf & s Next printf = Replace(printf, "\n", vbCrLf) printf = Replace(printf, "\t", vbTab) End Function
[此贴子已经被作者于2022-8-29 13:43编辑过]