📄 frmstring.frm
字号:
VERSION 5.00
Begin VB.Form frmString
Caption = "字符串测试"
ClientHeight = 5670
ClientLeft = 60
ClientTop = 345
ClientWidth = 8100
LinkTopic = "Form1"
ScaleHeight = 5670
ScaleWidth = 8100
StartUpPosition = 3 'Windows Default
Begin VB.TextBox txtFind
Height = 375
Left = 6240
TabIndex = 20
Top = 4320
Width = 1215
End
Begin VB.TextBox txtNum
Height = 375
Left = 6720
TabIndex = 17
Top = 3600
Width = 735
End
Begin VB.TextBox txtLength
Height = 375
Left = 5760
TabIndex = 15
Top = 2400
Width = 1695
End
Begin VB.TextBox txtResult
Height = 495
Left = 4680
TabIndex = 10
Top = 4920
Width = 2775
End
Begin VB.CommandButton cmdReplace
Caption = "Replace测试"
Height = 375
Left = 6000
TabIndex = 9
Top = 3000
Width = 1215
End
Begin VB.CommandButton cmdMid
Caption = "Mid替换测试"
Height = 375
Left = 3840
TabIndex = 8
Top = 3000
Width = 1335
End
Begin VB.CommandButton cmdStrConv
Caption = "StrConv求长度"
Height = 375
Left = 5760
TabIndex = 7
Top = 1800
Width = 1335
End
Begin VB.CommandButton cmdLen
Caption = "Len求长度"
Height = 375
Left = 3840
TabIndex = 6
Top = 1800
Width = 1335
End
Begin VB.CommandButton cmdInStrRev
Caption = "InStrRev测试"
Height = 375
Left = 1920
TabIndex = 5
Top = 1800
Width = 1215
End
Begin VB.CommandButton cmdInStr
Caption = "InStr测试"
Height = 375
Left = 360
TabIndex = 4
Top = 1800
Width = 1215
End
Begin VB.TextBox txtMatch
Height = 375
Left = 5880
TabIndex = 3
Top = 960
Width = 1575
End
Begin VB.TextBox txtStart
Height = 375
Left = 3000
TabIndex = 2
Top = 960
Width = 975
End
Begin VB.ListBox lstResult
Height = 2985
Left = 360
TabIndex = 1
Top = 2400
Width = 2655
End
Begin VB.TextBox txtSource
Height = 495
Left = 3000
TabIndex = 0
Top = 240
Width = 4455
End
Begin VB.Label lblFind
Caption = "Replace函数的搜索字符串:"
Height = 375
Left = 3600
TabIndex = 19
Top = 4320
Width = 2415
End
Begin VB.Label lblResult
Caption = "替换结果:"
Height = 495
Left = 3600
TabIndex = 18
Top = 4920
Width = 1095
End
Begin VB.Label lblNum
Caption = "输入Mid函数替换字符个数,或是Replace函数搜索次数,请输入整数:"
Height = 375
Left = 3600
TabIndex = 16
Top = 3600
Width = 3015
End
Begin VB.Label lblLength
Caption = "返回字符串长度:"
Height = 255
Left = 3600
TabIndex = 14
Top = 2400
Width = 1455
End
Begin VB.Label lblMatch
Caption = "用于替换的字符串:"
Height = 375
Left = 4080
TabIndex = 13
Top = 960
Width = 1695
End
Begin VB.Label lblStart
Caption = "输入开始位置,请输入整数:"
Height = 375
Left = 360
TabIndex = 12
Top = 960
Width = 2415
End
Begin VB.Label lblSource
Caption = "输入源字符串:"
Height = 375
Left = 360
TabIndex = 11
Top = 240
Width = 1575
End
End
Attribute VB_Name = "frmString"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub cmdInStr_Click()
Dim strSource As String
Dim iCount As Integer
Dim iFoundPos As Integer
Const SPACECHAR = " " '空格分割
strSource = Trim$(txtSource.Text)
If Len(strSource) = 0 Then
Exit Sub
End If
'txtStart用来输入要搜索字符串的起始位置,如果为空,则取默认值1
'在本程序中,主要用默认值即可
If Trim$(txtStart.Text) <> vbNullString Then
iCount = CLng(Trim$(txtStart.Text))
Else
iCount = 1
End If
iFoundPos = InStr(iCount, strSource, SPACECHAR)
lstResult.Clear
Do While iFoundPos <> 0
lstResult.AddItem Mid$(strSource, iCount, iFoundPos - iCount)
iCount = iFoundPos + 1
iFoundPos = InStr(iCount, strSource, SPACECHAR)
Loop
'循环结束后,仍有空格后的单词存留,则用一下语句来输出到List控件中
If iCount < Len(strSource) Then
lstResult.AddItem Mid$(strSource, iCount)
End If
End Sub
Private Sub cmdInStrRev_Click()
Dim strSource As String
Dim iCount As Integer
Dim iFoundPos As Integer
Const SPACECHAR = " " '空格分割
strSource = Trim$(txtSource.Text)
If Len(strSource) = 0 Then
Exit Sub
End If
'由于是反向搜索,因此iCount取整个字符串的长度
iCount = Len(strSource)
'搜索空格
iFoundPos = InStrRev(strSource, SPACECHAR, iCount)
lstResult.Clear
Do While iFoundPos <> 0
lstResult.AddItem Mid$(strSource, iFoundPos + 1, iCount - iFoundPos)
iCount = iFoundPos - 1
iFoundPos = InStrRev(strSource, SPACECHAR, iCount)
Loop
'循环结束后,仍有空格后的单词存留,则用一下语句来输出到List控件中
If iCount > 0 Then
lstResult.AddItem Left$(strSource, iCount)
End If
End Sub
Private Sub cmdLen_Click()
Dim strLen As String
Dim Ch As String
Dim N As Integer, i As Integer
N = 0
strLen = Trim$(txtSource.Text)
For i = 1 To Len(strLen) 'Len函数能够取得字符串中字符的个数
Ch = Asc(Mid(strLen, i, 1)) '取得第I个字符的字符码,Mid是提取字符串函数
If Ch >= 0 And Ch < 128 Then '此时,Ch是英文字符
N = N + 1
Else
N = N + 2 '如果是中文,则长度加2
End If
Next i
txtLength.Text = N
End Sub
Private Sub cmdMid_Click()
Dim strMid As String
Dim iPos As Long
Dim iCount As Long
On Error GoTo err
strMid = txtSource.Text
'txtStart用来输入要替换字符串的起始位置,如果为空,则取默认值1
If Trim$(txtStart.Text) <> vbNullString Then
iPos = CLng(Trim$(txtStart.Text))
Else
iPos = 1
End If
'用来检测txtNum文本框,如果里面有数字,则表示调用带指定替换字符个数的
'可选参数的Mid函数,否则就用不带该参数
If Trim$(txtNum.Text) <> vbNullString Then
iCount = CLng(Trim$(txtNum.Text))
Mid(strMid, iPos, iCount) = Trim$(txtMatch.Text)
Else
Mid(strMid, iPos) = Trim$(txtMatch.Text)
End If
txtResult.Text = strMid
Exit Sub
err:
MsgBox err.Number & " " & err.Description
End Sub
Private Sub cmdReplace_Click()
Dim strRepl As String
Dim iPos As Long
Dim iCount As Long
On Error GoTo err
strRepl = txtSource.Text
'txtStart用来输入要替换字符串的起始位置,如果为空,则取默认值1
If Trim$(txtStart.Text) <> vbNullString Then
iPos = CLng(Trim$(txtStart.Text))
Else
iPos = 1
End If
'如果txtNum文本框非空,则调用带可选参数的Replace函数,否则,就用不带可选参数
'的函数,此时,txtStart和txtNum中的内容为可选参数
If Trim$(txtNum.Text) <> vbNullString Then
iCount = CLng(Trim$(txtNum.Text))
strRepl = Replace(strRepl, Trim$(txtFind.Text), Trim$(txtMatch.Text), iPos, iCount)
Else
strRepl = Replace(strRepl, Trim$(txtFind.Text), Trim$(txtMatch.Text))
End If
txtResult.Text = strRepl
Exit Sub
err:
MsgBox err.Description
End Sub
Private Sub cmdStrConv_Click()
Dim strLen As String
Dim iLen As Integer
strLen = Trim$(txtSource.Text)
'该方法用来取得中英文混合字符串的长度比较简单,先用StrConv函数将“双字节”的字符串
'转换成中文占用2个Byte,英文占用1Byte,然后通过调用LenB就可以得到字符串的长度。
'如果此时用Len函数代替LenB,得到的长度值是真实长度的1/2
iLen = LenB(StrConv(strLen, vbFromUnicode))
txtLength.Text = iLen
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -