自己写的程序示例,没毛的狗
/images/2011/147787/2011051411021524.jpg" border="0" />
封装存取文本文件的一个例子
Private FileName_ As String '文件名
Private FileNumber_ As Variant '文件编号
Public Enum IOType
ForInput = 1
ForOutPut = 2
ForBinary = 3
End Enum
Private OpenType_ As IOType
Private OutPutText_ As Variant
Property Get OpenType() As IOType '读取打开类型属性
OpenType = OpenType_
End Property
Property Let OpenType(NewValue As IOType) '设置打开类型属性
OpenType_ = NewValue
End Property
Property Get FileName() As String '读取文件路径属性
FileName = FileName_
End Property
Property Let FileName(NewFileName As String) '读取文件路径属性
FileName_ = NewFileName
End Property
Property Get FileNumber() As Variant '读取文件编号属性
FileNumber = FileNumber_
End Property
Property Let FileNumber(NewFileNumber As Variant) '设置文件编号属性
FileNumber_ = NewFileNumber
End Property
Property Get OutPutText() As Variant '设置写入文件内容
OutPutText = OutPutText_
End Property
Property Let OutPutText(NewText As Variant) '读取写入文件内容
OutPutText_ = NewText
End Property
Public Function Enter() As Variant '方法
If OpenType = ForInput Then
Enter = For_Input
ElseIf OpenType = ForOutPut Then
For_Oput
' Else
End If
End Function
Private Function For_Input() As Variant '读取文件
Dim Str As Variant
Open FileName_ For Input As #FileNumber
While Not EOF(1)
Line Input #1, Str
For_Input = For_Input + Str + vbCrLf
Wend
Close #FileNumber
End Function
Private Function For_Oput() '写入文件
Open FileName_ For Output As #FileNumber
Print #FileNumber, OutPutText_
Close #FileNumber
End Function
模拟vb.net string类:
Private Text_ As String
Public Enum Str_Conv '字格式转换常数
FirstByteToUpper = 3 '3 第一个英文字大写
HalfToAll = 4 '4 所有半型字转全型字
AllToHalf = 8 '8 所有全型字转半型字
SingleCodeToUnicode = 64 '64 所有Single Code字转成UniCode字
UniCodeToSingleCode = 128 '128 所有UniCode字转成Single Code字
End Enum
Property Get Text() As String
Text = Text_
End Property
Property Let Text(ByVal NewText As String)
Text_ = NewText
End Property
Public Function GetLeft(ByVal LeftIndex As Integer) As String '截取字串左边某几个字符
GetLeft = Left(Text_, LeftIndex)
End Function
Public Function GetRight(ByVal RightIndex As Integer) As String '截取字串右边某几个字符
GetRight = Right(Text_, RightIndex)
End Function
Public Function GetMid(ByVal MidIndex As Integer, ByVal LenIndex As Integer) As String '截取字串右边某几个字符
GetMid = Mid(Text_, MidIndex, LenIndex)
End Function
Public Function ToLower() As String '字符串全转小写
ToLower = LCase(Text_)
End Function
Public Function ToUpper() As String '字符串全转大写
ToUpper = UCase(Text_)
End Function
Public Function SetStrConv(ByVal TextConv As Str_Conv) As String '字格式转换
SetStrConv = StrConv(Text_, TextConv)
End Function
Public Function InStrIndex(ByVal SubText As String) As Integer '传回字串里某个字串在第几个字的位置
InStrIndex = InStr(Text_, SubText)
End Function
Public Function InStrBIndex(ByVal SubBText As String) As Integer '传回字串里某个字串在第几个字符的位置
InStrBIndex = InStrB(Text_, SubBText)
End Function
Public Function GetLen() As Integer '传回字串有多少字数
GetLen = Len(Text_)
End Function
Public Function GetLenB() As Integer '传回字串有多少字符
GetLenB = LenB(Text_)
End Function
Public Function InStrRevIndex(ByVal InstrRevText As String, ByVal StartFindIndex As Integer) As String 'InStrRev是从起始位置开始倒着找
InStrRevIndex = InStrRev(InstrRevText, StartFindIndex)
End Function
Public Function ToLTrim() As String '删除字串左边的空白字串
ToLTrim = LTrim(Text_)
End Function
Public Function ToRTrim() As String 'RTrim 删除字串右边的空白字串
ToRTrim = RTrim(Text_)
End Function
Public Function ToTrim() As String 'Trim 删除字串首尾的空白字串
ToTrim = Trim(Text_)
End Function
Public Function SetReplace(ByVal WillFindString As String, ByVal WillReplaceString As String, ByVal StartIndex As Integer, ByVal GroundIndex As Integer) As String
SetReplace = Replace(Text_, WillFindString, WillReplaceString, StartIndex, GroundIndex)
End Function '取代自串中某些字符串
Public Function LetStrReverse() As String '反转字符串
LetStrReverse = StrReverse(Text_)
End Function