怎样取得一个字符串在另外一个字符串中出现的次数?
Public Function sCount(String1 As String, String2 As String) As Integer
Dim I As Integer, iCount As Integer
I = 1
Do
If (I > Len(String1)) Then Exit Do
I = InStr(I, String1, String2, vbTextCompare)
If I Then
iCount = iCount + 1
I = I + 2
DoEvents
End If
Loop While I
sCount = iCount
End Function
□ 怎样在一个字符串中完全删除里面的另外一个字符串?
Function StringCleaner(s As String, _
Search As String) As String
Dim i As Integer, res As String
res = s
Do While InStr(res, Search)
i = InStr(res, Search)
res = Left(res, i - 1) & _
Mid(res, i + 1)
Loop
StringCleaner = res
End Function
□ 怎样在一个字符串中删除里面的另外一个字符串?
Public Sub sRemove(String1 As String, String2 As String)
Dim I As Integer
I = 1
Do
If (I > Len(String1)) Then Exit Do
I = InStr(1, String1, String2)
If I Then
String1 = Left$(String1, I - 1) + Mid$(String1, I + Len(String2)+1)
I = I + 2
DoEvents
End If
Loop While I
End Sub
□ 怎样在一个字符串中替换里面的另外一个字符串?
Public Sub sReplace(String1 As String, String2 As String, RepString As String)
Dim I As Integer
I = 1
Do
If (I > Len(String1)) Then Exit Do
I = InStr(1, String1, String2)
If I Then
String1 = Left$(String1, I - 1) + RepString + Mid$(String1, I + Len(String2) )
I = I + 2
DoEvents
End If
Loop While I
End Sub
□ 如何计算一个字符串中的行数?
Function CountStringLine(src_string As String) As Integer
On Error Resume Next
Dim string_flag As Integer
Dim line_cnt As Integer
Dim test_string As String
line_cnt = 0 '初始--> 行数为1
string_flag = 1 '标志为1
test_string = src_string
DoEvents
Do
line_cnt = line_cnt + 1
string_flag = InStr(test_string, vbCrLf) '判断回车换行
test_string = Right(test_string, Len(test_string) - string_flag - 1)
Loop Until string_flag <= 0
CountStringLine = line_cnt
End Function
□ 如何从一个字符串中读取一行字符?
Function ReadStringLine(src_str As String, lineno As Integer) As String
On Error Resume Next
Dim string_flag As Integer
Dim line_cnt As Integer
Dim test_string As String
Dim ret_string As String
line_cnt = 0 '初始--> 行数为1
string_flag = 1 '标志为1
test_string = Right(src_str, 2)
If test_string <> vbCrLf Then
test_string = src_str + vbCrLf
Else
test_string = src_str
End If
DoEvents
Do
line_cnt = line_cnt + 1
string_flag = InStr(test_string, vbCrLf)
ret_string = Left(test_string, string_flag)
test_string = Right(test_string, Len(test_string) - string_flag - 1)
Loop Until lineno <= line_cnt
'If line_cnt = 1 Then
' ReadStringLine = ret_string
'Else
ReadStringLine = Left(ret_string, Len(ret_string) - 1)
'End If
End Function
- 还没有人评论,欢迎说说您的想法!