Sie sind auf Seite 1von 6

Visual Basic version 6.0 introduces a number of new string functions to simplify string manipulation in your code.

This article describes how the same functionality can be used with earlier versions of Visual Basic. MORE INFORMATION

The following additional string functions are present in Visual Basic 6.0:

Function -------Join Split InStrRev Replace Reverse

Description ----------Used to join arrays elements. Split a string into a variant array. Similar to InStr but searches from end of string. To find a particular string and replace it. To reverse a string.

If these functions are coded and added in a module, all the above functions can be used in the project as if they are intrinsic functions.

1. 2.

Start a new project. Form1 is created by default. Add a new module to the project and add the following

code to the module:

3. 4. 5. Optional _ 6. 7. 8. 9. 10.

Option Explicit Public Function Join(source() As String, sDelim As String = " ") As String Dim sOut As String, iC As Integer On Error GoTo errh: For iC = LBound(source) To sOut = sOut & source(iC) & sDelim

UBound(source) - 1

11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. nC As Integer 23. 24. 25. 26. bCompare) 27. 28. 29. 30. 31. Then Exit Do 32. 33. 34. 35. 36. 37. 38. 39. String, _

Next sOut = sOut & source(iC) Join = sOut Exit Function errh: Err.Raise Err.Number End Function Public Function Split(ByVal sIn As String, String, Optional nLimit As Long = -1, VbCompareMethod = Dim sRead As String, sOut() As String, If sDelim = "" Then Split = sIn End If sRead = ReadUntil(sIn, sDelim, Do ReDim Preserve sOut(nC) sOut(nC) = sRead nC = nC + 1 If nLimit <> -1 And nC >= nLimit sRead = ReadUntil(sIn, sDelim) Loop While sRead <> "" ReDim Preserve sOut(nC) sOut(nC) = sIn Split = sOut End Function Public Function ReadUntil(ByRef sIn As

Optional sDelim As _ Optional bCompare As _ vbBinaryCompare) As Variant

40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. bCompare) 65. 66. 67. 68. +2

sDelim As String, Optional bCompare = vbBinaryCompare) As String Dim nPos As String nPos = InStr(1, sIn, sDelim, bCompare) If nPos > 0 Then ReadUntil = Left(sIn, nPos - 1) sIn = Mid(sIn, nPos + Len(sDelim)) End If End Function Public Function StrReverse(ByVal sIn As Dim nC As Integer, sOut As String For nC = Len(sIn) To 1 Step -1 sOut = sOut & Mid(sIn, nC, 1) Next StrReverse = sOut End Function Public Function InStrRev(ByVal sIn As Optional nStart As Long = 1, Optional VbCompareMethod = Dim nPos As Long sIn = StrReverse(sIn) sFind = StrReverse(sFind) nPos = InStr(nStart, sIn, sFind, If nPos = 0 Then InStrRev = 0 Else InStrRev = Len(sIn) - nPos - Len(sFind)

As VbCompareMethod _

String) As String

String, sFind As String, _ bCompare As _ vbBinaryCompare) As Long

69. 70. 71. 72. As String, _ 73. Long = 1, _ 74. bCompare As _ 75. 76. 77. As String 78. 79. bCompare) 80. 81. 82. 83. &_ 84. 85. Then Exit Do 86. bCompare) 87. 88. 89. 90. 91.

End If End Function Public Function Replace(sIn As String, sFind sReplace As String, Optional nStart As Optional nCount As Long = -1, Optional VbCompareMethod =

vbBinaryCompare) As String Dim nC As Long, nPos As Integer, sOut sOut = sIn nPos = InStr(nStart, sOut, sFind, If nPos = 0 Then GoTo EndFn: Do nC = nC + 1 sOut = Left(sOut, nPos - 1) & sReplace Mid(sOut, nPos + Len(sFind)) If nCount <> -1 And nC >= nCount nPos = InStr(nStart, sOut, sFind, Loop While nPos > 0 EndFn: Replace = sOut End Function

92.

Add two CommandButtons to Form1. Add the following

code to the General Declaration section of Form1:

93.

Option Explicit 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. Private Sub Form_Load() Command1.Caption = "Join" Command2.Caption = "Split" End Sub Private Sub Command2_Click() Dim i As Integer MyVar = Split(MyStr, ";") For i = LBound(MyVar) To Debug.Print MyVar(i) Next End Sub Private Sub Command1_Click() MyArray(0) = "item1" MyArray(1) = "item2" MyArray(2) = "item3" MyStr = Join(MyArray, ";") Debug.Print MyStr End Sub Dim MyArray(2) As String, MyStr As String, MyVar As Variant

UBound(MyVar)

118.

Press the F5 key to run the project. Click on Join, and

note in the debug window that the array elements are joined. Click on Split to split a string into a variant array. 119. Pause the project by pressing the CTRL+BREAK key

combination or clicking the Pause button on the toolbar. Test the remaining functions by typing the following lines in the immediate window and verifying the results:

?instrrev("//drive/dir","/") 8

?strreverse("input string") gnirts tupni

?replace("input string"," ", "-",4,1) input-string

NOTE: The errors generated may not exactly coincide with those generated by the Visual Basic 6.0 intrinsic string functions.

Das könnte Ihnen auch gefallen