format后面的字符串消失了

diaryfly 2013-08-22 03:19:42
将几个字符串(str1-str6)合并输出时碰到问题:

Text1.text = str1 & "\" & str2 & "\" & str3 & "\" & str4 & "-" & str5 & "\" & Format(time, "hh:mm") & "-" & "\" & str6

文本只能输出到str5以前的字符串。format后面的就消失了。
如果换成:
Text2.text = "str1" & "\" & "str2" & "\" & "str3" & "\" & "str4" & "-" & "str5" & "\" & Format(time, "hh:mm") & "\" & "str6"

又没有问题。求大侠指导。
...全文
145 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
diaryfly 2013-08-23
  • 打赏
  • 举报
回复
谢谢大侠的解答,
  • 打赏
  • 举报
回复
因为中文"非"占用了二个字节,byte字节 j = GetPrivateProfileString("test1", "5", " ", str, 50, s) 因此 j = 2 而对于 str 字符,实际 "非"只有一个字节 str5 = Mid(str, 1, j) 因此上面的 str5的数据就出现了问题 Str5 = "非" + Chr(0)
diaryfly 2013-08-23
  • 打赏
  • 举报
回复
谢谢4楼。我还是没有明白,中文字符串怎么就出错了呢?是因为GetPrivateProfileString读到中文就在后面加char(0)吗?GetPrivateProfileString适合用在什么地方?
  • 打赏
  • 举报
回复
问题出在读取ini文件内容的过程 GetPrivateProfileString 有问题 请用我一直用的读取与写入ini文件的试试看<二个方法可以放入模块中>


Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal pFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFilename As String) As Long
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFilename As String) As Long


Private Sub Form_Load()
   Dim s As String, str As String, str1 As String, str2 As String, str3 As String, str4 As String, str5 As String, str6 As String
    Dim i As Integer, j As Integer
    On Error Resume Next
    s = "F:\test.INI"
    str = Space(50)
    str0 = i
    str1 = GetIniStr(s, "test1", "1")
    str2 = GetIniStr(s, "test1", "2")
    str3 = GetIniStr(s, "test1", "3")
    str4 = GetIniStr(s, "test1", "4")
    str5 = GetIniStr(s, "test1", "5")
    str6 = GetIniStr(s, "test1", "6")

Text1.Text = str1 & "\" & str2 & "\" & str3 & "\" & str4 & "-" & str5 & "\" & Format(Time, "hh:ss") & "\" & str6
            

End Sub




'*************************************
   '目的:写入数据至Ini文件
   
   '输入: FileName 文件名
   '      AppName  项目名
   '      In_Key   键名
   '      In_Data  键名上的数值
   
   '返回:  写入成功 True
   '       写入失败 False
   
'*************************************

Public Function WriteIniStr(ByVal FileName As String, ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
   GoTo WriteIniStrErr
Else
   WritePrivateProfileString AppName, In_Key, In_Data, FileName
End If
Exit Function
WriteIniStrErr:
   Err.Clear
   WriteIniStr = False
End Function


'*************************************
   '目的:从Ini文件中读取数据
   
   '输入: FileName 文件名
   '      AppName  项目名
   '      In_Key   键名
   
   '返回: 取得给定键名上的数据
   
'*************************************

Public Function GetIniStr(ByVal FileName As String, ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
   GoTo GetIniStrErr
End If
Dim GetStr As String
  GetStr = VBA.String(128, 0)
  GetPrivateProfileString AppName, In_Key, "", GetStr, 256, FileName
  GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
   GoTo GetIniStrErr
Else
   GetIniStr = GetStr
   GetStr = ""
End If
Exit Function
GetIniStrErr:
   Err.Clear
   GetIniStr = ""
   GetStr = ""
End Function



diaryfly 2013-08-22
  • 打赏
  • 举报
回复
谢谢1,2楼的大侠;我没有描述清楚,请看下面代码:

Private Sub Form_Load()
Dim s As String, str As String, str1 As String, str2 As String, str3 As String, str4 As String, str5 As String, str6 As String
Dim i As Integer, j As Integer
On Error Resume Next
s = App.Path & "\test.INI"
str = Space(50)
str0 = i
j = GetPrivateProfileString("test1", "1", " ", str, 50, s)
str1 = Mid(str, 1, j)
j = GetPrivateProfileString("test1", "2", " ", str, 50, s)
str2 = Mid(str, 1, j)
j = GetPrivateProfileString("test1", "3", " ", str, 50, s)
str3 = Mid(str, 1, j)
j = GetPrivateProfileString("test1", "4", " ", str, 50, s)
str4 = Mid(str, 1, j)
j = GetPrivateProfileString("test1", "5", " ", str, 50, s)
str5 = Mid(str, 1, j)

j = GetPrivateProfileString("test1", "6", " ", str, 50, s)
str6 = Mid(str, 1, j)

Text1.Text = str1 & "\" & str2 & "\" & str3 & "\" & str4 & "-" & str5 & "\" & str6


End Sub


以下是test.ini文件内容:


[test1]

1=a
2=b
3=c
4=d
5=非
6=f





a\b\c\d-非

如上
遇到中文后面的就没有了。如果是e文就没有问题
Tiger_Zhao 2013-08-22
  • 打赏
  • 举报
回复
变量 str5 中含有字符 Chr(0)。
Text1.Text = "abc" & Chr(0) & "def" '只显示 abc'
  • 打赏
  • 举报
回复
str1至str5 分别什么?我试了一下,我可以正常.

Private Sub Form_Load()

Dim str1
Dim str2
Dim str3
Dim str4
Dim str5
Dim Str6
str1 = 6
str2 = 7
str3 = 8
str4 = 9
str5 = 10
Str6 = 11

Text1.Text = str1 & "\" & str2 & "\" & str3 & "\" & str4 & "-" & str5 & "\" & Format(Time, "hh:mm") & "-" & "\" & Str6

End Sub

7,762

社区成员

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

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