VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - VB问题全功略(59)
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:11273, 日期:2002-02-03
VB问题全功略(59)

291、如何去掉文中多余的回车和空行?
292、如何在每一个中文后面加一个空格?
293、如何匹配TextBox框的查找下一个功能?
294、如何寻找并加亮找到的字符?
295、如何移去字符串末端的目录符号\?

291、如何去掉文中多余的回车和空行?
注释:下面的函数可以去掉文中多余的回车和空行,可以对付非常规的字符(以0Ah作为回车,而不是0Dh,0Ah)
Private Function FormatStr(strReadyToFormat As String) As String
Dim strTemp() As String
Dim strReady As String
Dim nPos As Long
Dim i As Long
On Error Resume Next
Do
DoEvents
注释:有的文件以0Ah作为回车换行标志
nPos = InStr(1, strReadyToFormat, Chr(10), vbBinaryCompare)
注释:找到0AH后,表示准备另起一行,先将之前的字符0Dh取出(如果有的话),0Dh表示回车
strReady = Left(strReadyToFormat, nPos - 1)
注释:如果前面有0DH,全部去掉
Do While Asc(Right(strReady, 1)) = 13
strReady = Left(strReady, Len(strReady) - 1)
If strReady = "" Then Exit Do
Loop
注释:检查是不是一个空行
If Trim(strReady) <> "" Then
注释:若是,则写入
i = i + 1
ReDim Preserve strTemp(i)
strTemp(i) = strReady
End If
注释:去掉头部的字符串
strReadyToFormat = Right(strReadyToFormat, Len(strReadyToFormat) - nPos)
Loop Until nPos = 0 注释:继续向下找
FormatStr = ""
For i = 1 To UBound(strTemp)
FormatStr = FormatStr + strTemp(i)
Next
End Function
End Function

292、如何在每一个中文后面加一个空格?

Dim TEXTlen As Integer
Dim i As Integer
Dim temp1 As String
Dim temp2 As String
Dim MyCreate As String
Dim j As Integer
Dim NextLine As String
Command1.Enabled = False
If List1.ListCount = 0 Then Exit Sub
Form1.MousePointer = 11
For j = 0 To List1.ListCount - 1
Label2.Caption = "共 " & Str(List1.ListCount) & "个文件,正在修改第 " & Str(j + 1) & " 个文件。"
注释:打开一个文件,input方式 #1
Open List1.List(j) For Input As #1
注释:打开一个文件,append文件 #2
Open (App.Path & "\temp.tmp") For Append As #2
Do Until EOF(1)
注释:从#1读取一行
Line Input #1, NextLine
MyCreate = ""
TEXTlen = Len(NextLine)
For i = 1 To TEXTlen
temp1 = Mid(NextLine, i, 1)
If Asc(temp1) < 0 Then
temp2 = Mid(NextLine, i + 1, 1)
If temp2 <> " " Then
temp1 = temp1 & " "
End If
End If
MyCreate = MyCreate + temp1
DoEvents
Next
Print #2, MyCreate
注释:向#2写文件
Loop
Close #1
Close #2
FileCopy App.Path & "\temp.tmp", List1.List(j)
Kill App.Path & "\temp.tmp"
Next 注释:下一个文件
Form1.MousePointer = 1
MsgBox "文件已经成功修改!", vbOKOnly + vbInfoBackground, "恭喜!"

293、如何匹配TextBox框的查找下一个功能?

If KeyCode = vbKeyF3 Then 注释:F3查找下一个
注释:下面这个If块在查找下一个匹配字符时很有用
If txtContext.SelStart = 0 Then 注释:光标位置在文本框最开头
If txtContext.SelLength > 0 Then
nPos = 2 注释:如果文本框中有被加亮的字符
Else
nPos = 1 注释:注释:如果文本框中没有被加亮的字符
End If
Else
If txtContext.SelLength > 0 Then
nPos = txtContext.SelStart + 2 注释:如果文本框中有被加亮的字符
Else
nPos = txtContext.SelStart + 1 注释:如果文本框中没有被加亮的字符
End If
End If
nPos = InStr(nPos, txtContext.Text, FrmSearch.txtSearch.Text, vbTextCompare)
If nPos = 0 Then Exit Sub 注释:nPos=0表示没有找到
注释:加亮找到的字符串
txtContext.SelStart = nPos - 1
txtContext.SelLength = Len(FrmSearch.txtSearch.Text)

294、如何寻找并加亮找到的字符?

If KeyCode = vbKeyF3 Then 注释:F3查找下一个
注释:下面这个If块在查找下一个匹配字符时很有用
If txtContext.SelStart = 0 Then 注释:光标位置在文本框最开头
If txtContext.SelLength > 0 Then
nPos = 2 注释:如果文本框中有被加亮的字符
Else
nPos = 1 注释:注释:如果文本框中没有被加亮的字符
End If
Else
If txtContext.SelLength > 0 Then
nPos = txtContext.SelStart + 2 注释:如果文本框中有被加亮的字符
Else
nPos = txtContext.SelStart + 1 注释:如果文本框中没有被加亮的字符
End If
End If
nPos = InStr(nPos, txtContext.Text, FrmSearch.txtSearch.Text, vbTextCompare)
If nPos = 0 Then Exit Sub 注释:nPos=0表示没有找到
注释:加亮找到的字符串
txtContext.SelStart = nPos - 1
txtContext.SelLength = Len(FrmSearch.txtSearch.Text)

295、如何移去字符串末端的目录符号\?

Public Function RemoveBackslash(s As String) As String
Dim i As Integer
i = Len(s)
If i <> 0 Then
If Right$(s, 1) = "\" Then
RemoveBackslash = Left$(s, i - 1)
Else
RemoveBackslash = s
End If
Else
RemoveBackslash = ""
End If
End Function