带有汉字的字符串读写问题

wolfloveu 2004-07-29 10:59:54
我的原文件如下:
中国,hello,山东,shandong
中国海,hello,山东省,shandong
中国海洋,hello,山东省会,shandong

我期待的结果是:
中国 hello 山东 shandong
中国海 hello 山东省 shandong
中国海洋 hello 山东省会 shandong
格式为a(10) a(7) a(10) a(8)

请各位大虾帮帮忙,最好有代码,跪谢!
...全文
194 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
落伍者 2004-07-29
  • 打赏
  • 举报
回复
以上代码测试通过
落伍者 2004-07-29
  • 打赏
  • 举报
回复


private type myRecord
str1 as string *10
str2 as string * 7
str3 as string * 10
str 4 as string * 8
end type

Dim xx As myRecord
Dim strArr
strArr = Array("中国", "hello", "山东", "shandong")
xx.str1 = strArr(0)
xx.str2 = strArr(1)
xx.str3 = strArr(2)
xx.str4 = strArr(3)

Dim byt() As Byte
With Picture1
Open "d:\1.txt" For Random Access Write As #1
Put #1, , xx
Close #1
End With

liuyan4794 2004-07-29
  • 打赏
  • 举报
回复
可以自定义函数分割,不用vb的
aliaoz 2004-07-29
  • 打赏
  • 举报
回复
算字符长度,然后再根据长度填空格。
wolfloveu 2004-07-29
  • 打赏
  • 举报
回复
我自己的方法是先定义一个空格字符串
strTemp=space(35)
然后用分割后的单词去填充它所在的位置,字母当然没有什么问题,可是遇到汉字的时候,填充进去的时候只占一个空格,但是写出来时位置便错行。
假设已将原文的字符取出付给一个字符数组strArr(4)
mid(strTemp,1,10)=strArr(0) & space(10-len(strArr(0)))
mid(strTemp,11,7)=strArr(1) & space(7-len(strArr(1)))
mid(strTemp,18,10)=strArr(2) & space(10-len(strArr(2)))
mid(strTemp,28,8)=strArr(3) & space(8-len(strArr(3)))
写出的结果如下:
中国 hello 山东 shandong
中国海 hello 山东省 shandong
中国海洋 hello 山东省会 shandong
烦请各位大虾帮助解决。
laisiwei 2004-07-29
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Form_Load()
Debug.Print GetGs("中国 , hello, 山东, shandong")
Debug.Print GetGs("中国海 , hello, 山东省, shandong")
Debug.Print GetGs("中国海洋 , hello, 山东省会, shandong")
End Sub
Private Function GetGs(a As String) As String
Dim b() As String
Dim c As String
b = Split(a, ",")
b(0) = Trim(b(0))
c = c & b(0) & String(10 - LenB(StrConv(b(0), vbFromUnicode)), " ")
b(1) = Trim(b(1))
c = c & b(1) & String(7 - LenB(StrConv(b(1), vbFromUnicode)), " ")
b(2) = Trim(b(2))
c = c & b(2) & String(10 - LenB(StrConv(b(2), vbFromUnicode)), " ")
b(3) = Trim(b(3))
c = c & b(3) & String(8 - LenB(StrConv(b(3), vbFromUnicode)), " ")
GetGs = c
End Function
TechnoFantasy 2004-07-29
  • 打赏
  • 举报
回复
然后调用Format函数格式化分割后的单词。
TechnoFantasy 2004-07-29
  • 打赏
  • 举报
回复
可以使用split函数分割成单词。
熊孩子开学喽 2004-07-29
  • 打赏
  • 举报
回复
没有调试过,楼主自己试试吧

熊孩子开学喽 2004-07-29
  • 打赏
  • 举报
回复
sub FormatString(TXT1 as string,TXT2 as string,TXT3 as string)
dim Str,Str1,Str2
dim I as long
dim L as long
str=split(TXT1,",",-1)
str1=split(TXT2,",",-1)
str2=split(TXT3,",",-1)
for i=lbound(str) to ubound(str)
if Len(str(I))>L then L=Len(str(I))
if Len(str1(I))>L then L=Len(str1(I))
if Len(str2(I))>L then L=Len(str2(I))
next
for i=lbound(str) to ubound(str)
TXT1=TXT1 & str(I) & space(L-len(str(I)))
TXT2=TXT2 & str1(I) & space(L-len(str1(I)))
TXT3=TXT3 & str2(I) & space(L-len(str2(I)))
next
end sub
试试这个吧
northwolves 2004-07-29
  • 打赏
  • 举报
回复
Function getit(ByVal x As String) As String
Dim temp() As String
temp = Split(x, ",")
For i = 0 To UBound(temp)
temp(i) = Trim(temp(i)) & StrConv(Space(Choose(i + 1, 5, 7, 5, 8) - Len(Trim(temp(i)))), vbUnicode)
Next
getit = Join(temp, "")
getit = getit & " ,length=" & Len(getit)
End Function
Private Sub Command1_Click()
Debug.Print getit("中国 , hello, 山东, shandong")
Debug.Print getit("中国海 , hello, 山东省, shandong")
Debug.Print getit("中国海洋 , hello, 山东省会, shandong")

End Sub
ryuginka 2004-07-29
  • 打赏
  • 举报
回复
up
rainstormmaster 2004-07-29
  • 打赏
  • 举报
回复
用midb函数

7,762

社区成员

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

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