这样的字符串如何格式化?

fangyds 2003-09-15 11:35:31
有一系列字符串,如下:
a abcde abcdefghijklmn
b abcdefghkogla sladkfjsldkfjl;asdkfj;sd
c as alsdjkf;asldkfj;asldkfjsadklf;lsadfkasl;dfkj
d asl;dkfjas;ldkf;asldfkas;dklf sl;adkfjas;dlfjk;asdljkf
......
也就是说除第一列字符串长度固定外,其它列的字符串的长度都不固定,中间用TAB(或空格)隔开。
现在我想把它们格式化成这样:
a abcde abcdefghijklmn
b abcdefghkogla sladkfjsldkfjl;asdkfj;sd
c as alsdjkf;asldkfj;asldkfjsadklf;lsa
d asl;dkfjas;ldkf;asldfkas;dklf sl;adkfjas;dlfjk;asdljkf
......
不知道我说明白没有,如果有什么不明白的请提出。谢谢!
...全文
93 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
摇摆的小鸭子 2010-06-17
  • 打赏
  • 举报
回复
有很多的例子:例如在c#里可以这样输出时间:.Tostring("yyyymmm")
rainstormmaster 2003-09-16
  • 打赏
  • 举报
回复
northwolves(野性的呼唤)的方法可行,不妨一试
fangyds 2003-09-16
  • 打赏
  • 举报
回复
'以下内容在模块中
'判断指定的文件或路径是否存在
Public Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long

'lenx是指各个列的最长字符串的长度,strs是要输出的字符串
Public Type strOutput
strs As String
len1 As Integer
len2 As Integer
len3 As Integer
len4 As Integer
End Type

'检查类型
Public Enum CheckType
[ctAll] = 0 '检查所有内容
[ctFileExist] = 1 '检查文件是否存在
[ctLyricExist] = 2 '检查歌词是否存在
End Enum

Public Function Output(snlen As Integer, outputstr As strOutput) As String
Dim y() As String
With outputstr
y = Split(.strs, " ")
If snlen < 2 Then
Output = y(0) & Space(6 - Len(y(0))) & y(1) & Space(.len1 + 4 - (LenB(StrConv(y(1), vbFromUnicode)))) & y(2) & Space(.len2 + 4 - (LenB(StrConv(y(2), vbFromUnicode)))) & y(3) & Space(.len3 + 4 - (LenB(StrConv(y(3), vbFromUnicode)))) & y(4) & Space(.len4 + 4 - (LenB(StrConv(y(4), vbFromUnicode)))) & y(5)
Else
Output = y(0) & Space(snlen + 4 - Len(y(0))) & y(1) & Space(.len1 + 4 - (LenB(StrConv(y(1), vbFromUnicode)))) & y(2) & Space(.len2 + 4 - (LenB(StrConv(y(2), vbFromUnicode)))) & y(3) & Space(.len3 + 4 - (LenB(StrConv(y(3), vbFromUnicode)))) & y(4) & Space(.len4 + 4 - (LenB(StrConv(y(4), vbFromUnicode)))) & y(5)
End If
End With
End Function

Public Function CheckPath(ByVal sPath As String) As String
On Error Resume Next

If Right$(sPath, 1) = "\" Then
CheckPath = sPath
Else
CheckPath = sPath & "\"
End If
End Function

Public Function FileExist(ByVal sFile As String) As Boolean
On Error Resume Next

Dim lngResult As Long
lngResult = PathFileExists(sFile)
FileExist = CBool(lngResult)
End Function
fangyds 2003-09-16
  • 打赏
  • 举报
回复
多谢各位的帮助,现将代码贴出来:
因为字符串包含中文字符,所以用了LenB和StrConv这两个函数
northwolves(野性的呼唤)的方法很好用,但有一个问题,如果固定用Space(40)的话,各列之间的距离会有大小,而且如果字符串的长度超过40的话,也会出现排列混乱,所以我又用foreverforyou(为情所困) 的方法,先求得每列中最长字符串的长度,这样就不会出现排列混乱了。
由于本人技业不高,所以代码的执行速度较慢。

Private Sub Check(ctCheckType As CheckType, ByVal strCheckSql As String)
On Error Resume Next

Dim i As Long '循环变量
Dim lngRecCount As Long '存放记录总数
Dim lngResultCount As Long '存放检查或搜索到的记录总数
Dim errItem As ListItem '
Dim strName As String '存放name字段的内容
Dim strSing As String '存放sing字段的内容
Dim strAlbum As String '存放album字段的内容
Dim strFile As String '存放file字段的内容
Dim lenname As Integer '存放name到目前为止最长字符串的长度
Dim lensing As Integer '存放sing到目前为止最长字符串的长度
Dim lenalbum As Integer '存放album到目前为止最长字符串的长度
Dim lenfile As Integer '存放file到目前为止最长字符串的长度

Cmd.CommandText = strCheckSql
Set Rec = Cmd.Execute
Rec.MoveLast
lngRecCount = Rec.RecordCount
Rec.MoveFirst
livResult.ListItems.Clear
cmdStart.Enabled = False
cmdFinish.Enabled = True

If optCheck.Value = True Then
labStatus.Caption = "正在检查..."
Else
labStatus.Caption = "正在搜索..."
End If

For i = 1 To lngRecCount '循环检查提取的所有记录
If boolfinish Then Exit For '如果按了结束按钮
With livResult
strFile = Rec.Fields("abpath")
If optCheck = True Then '如果是"检查"操作
If (ctCheckType = ctAll) Or (ctCheckType = ctFileExist) Then '如果是要检查文件是否存在或所有内容
If FileExist(strFile) = False Then '如果文件不存在
'为变量赋值
With Rec
strName = .Fields("name")
strSing = .Fields("sing")
strAlbum = .Fields("album")
End With
'检查字符串长度,如果大于原先记录的长度的话,就赋新值到变量
lenname = IIf(LenB(StrConv(strName, vbFromUnicode)) > lenname, LenB(StrConv(strName, vbFromUnicode)), lenname)
lensing = IIf(LenB(StrConv(strSing, vbFromUnicode)) > lensing, LenB(StrConv(strSing, vbFromUnicode)), lensing)
lenalbum = IIf(LenB(StrConv(strAlbum, vbFromUnicode)) > lenalbum, LenB(StrConv(strAlbum, vbFromUnicode)), lenalbum)

'在 ListView 中列出有错误的记录
Set errItem = .ListItems.Add(, , "歌曲不存在")
With errItem
.SubItems(1) = strName
.SubItems(2) = strSing
.SubItems(3) = strAlbum
.SubItems(4) = strFile
End With
End If
End If
If (ctCheckType = ctAll) Or (ctCheckType = ctLyricExist) Then '如果是要检查歌词是否存在或所有内容
strFile = "F:\Music\" & Replace(Rec.Fields("lyric"), "/", "\") '获取歌词文件的绝对路径
If FileExist(strFile) = False Then '如果文件不存在
'为变量赋值
With Rec
strName = .Fields("name")
strSing = .Fields("sing")
strAlbum = .Fields("album")
End With
'检查字符串长度,如果大于原先记录的长度的话,就赋新值到变量
lenname = IIf(Len(strName) > lenname, Len(strName), lenname)
lensing = IIf(Len(strSing) > lensing, Len(strSing), lensing)
lenalbum = IIf(Len(strAlbum) > lenalbum, Len(strAlbum), lenalbum)

'在 ListView 中列出有错误的记录
Set errItem = .ListItems.Add(, , "歌词不存在")
With errItem
.SubItems(1) = strName
.SubItems(2) = strSing
.SubItems(3) = strAlbum
.SubItems(4) = strFile
End With
End If
End If
'将得到的长度赋值给变量 strop,以备输出到文件时使用
With strop
.len1 = 10
.len2 = lenname
.len3 = lensing
.len4 = lenalbum
End With
Else '如果是搜索操作
'为变量赋值
With Rec
strName = .Fields("name")
strSing = .Fields("sing")
strAlbum = .Fields("album")
End With
'检查字符串长度,如果大于原先记录的长度的话,就赋新值到变量
lenname = IIf(LenB(StrConv(strName, vbFromUnicode)) > lenname, LenB(StrConv(strName, vbFromUnicode)), lenname)
lensing = IIf(LenB(StrConv(strSing, vbFromUnicode)) > lensing, LenB(StrConv(strSing, vbFromUnicode)), lensing)
lenalbum = IIf(LenB(StrConv(strAlbum, vbFromUnicode)) > lenalbum, LenB(StrConv(strAlbum, vbFromUnicode)), lenalbum)

'在 ListView 中列出搜索到的记录
Set errItem = .ListItems.Add(, , strName)
With errItem
.SubItems(1) = Rec.Fields("expand")
.SubItems(2) = strSing
.SubItems(3) = strAlbum
.SubItems(4) = strFile
End With
'将得到的长度赋值给变量 strop,以备输出到文件时使用
With strop
.len1 = lenname
.len2 = 3
.len3 = lensing
.len4 = lenalbum
End With
End If
End With
Rec.MoveNext '移动到下一要记录
Next i
cmdStart.Enabled = True
cmdFinish.Enabled = False
lngResultCount = livResult.ListItems.Count
If optCheck.Value = True Then
labStatus.Caption = "共检查到 " & lngResultCount & " 首有错误的歌曲"
Else
labStatus.Caption = "共搜索到 " & lngResultCount & " 首歌曲"
End If
If lngResultCount > 0 Then cmdOutput.Enabled = True
Set errItem = Nothing '释放资源
End Sub

'按下“输出”按钮
Private Sub cmdOutput_Click()
Dim strFile As String
Dim strInfo As String
Dim i As Long
Dim strSNLen As String
strFile = CheckPath(App.Path) & "List.txt"

Open strFile For Append As #1
With livResult
' Debug.Print strop.len1 & " " & strop.len2 & " " & strop.len3 & " " & strop.len4 & " " & strop.strs
strSNLen = String$(Len(CStr(.ListItems.Count)), "0")
strop.strs = "ID" & " " & .ColumnHeaders(1).Text & " " & .ColumnHeaders(2).Text & " " & .ColumnHeaders(3).Text & " " & .ColumnHeaders(4).Text & " " & .ColumnHeaders(5).Text
Print #1, Output(Len(strSNLen), strop)
For i = 1 To livResult.ListItems.Count
strInfo = Format$(i, strSNLen) & " " & .ListItems(i).Text & " " & .ListItems(i).SubItems(1) & " " & .ListItems(i).SubItems(2) & " " & .ListItems(i).SubItems(3) & " " & .ListItems(i).SubItems(4)
strop.strs = strInfo
Print #1, Output(Len(strSNLen), strop)
Next i
End With
Close #1
End Sub
northwolves 2003-09-15
  • 打赏
  • 举报
回复
Private Sub Form_Load()
Dim a(3) As String
a(0) = "a abcde abcdefghijklmn"
a(1) = "b abcdefghkogla sladkfjsldkfjl;asdkfj;sd"
a(2) = "c as alsdjkf;asldkfj;asldkfjsadklf;lsadfkasl;dfkj"
a(3) = "d asl;dkfjas;ldkf;asldfkas;dklf sl;adkfjas;dlfjk;asdljkf"
For i = 0 To 3
Debug.Print shuchu(a(i))
next
End Sub

Function shuchu(ByVal x As String) As String
Dim y
y = Split(x, " ")
shuchu = y(0) & Space(5) & y(1) & Space(40 - Len(y(1))) & Space(5) & y(2)
End Function

foreverforyou 2003-09-15
  • 打赏
  • 举报
回复
编程,判断每一列的最长的字符串的长度,按一个TAB四个字符位计算出每一行的该列还需要多少个TAB

7,763

社区成员

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

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