|
-
Nov 13th, 2000, 11:36 PM
#1
Thread Starter
Junior Member
Can anyone help with the code for this command button? Thanks.
Add a "Reverse Line" button. Reverse Line should take an entry such as
"This is some text"
and reverse it as follows:
"text some is This".
-
Nov 14th, 2000, 12:25 AM
#2
Addicted Member
Hi,
Try NewText.Text = ReverseString(Text1.Text)
Regards
-
Nov 14th, 2000, 01:44 AM
#3
Member
You forgot to supply your ReverseString function. VB does not have this function built in.
-
Nov 14th, 2000, 03:16 AM
#4
PowerPoster
Hi, The ReverseStr function is available in VB, but are you using VB 6.0?
By the way, the ReverseStr may not return what you looking for, because it will return the folloing result:
Source string = "This is some text"
Retrun string = "txet emos si sihT"
So, I have wrote a simple ReverseStr function by using the VBScript 5.0 or higher DLL and the code is show below:
Code:
Private Function ReverseStr(ByVal sourceStr As String) As String
Dim regex As RegExp
Dim col As MatchCollection
Dim match As match
Dim str() As String
Dim result As String
Dim length As Long
Dim Idx As Integer
'***************************************************************
' This example code need the VBScript.dll file
' Before you use this code make sure you have
' this DLL in your Windows system folder.
'
' You can download this DLL from the following URL
'http://www.microsoft.com/msdownload/vbscript/scripting51.asp
'
'***************************************************************
Set regex = New RegExp
With regex
.IgnoreCase = True
.Global = True
'\S* search for all non white space charater
'\s search for all space character
.Pattern = "\S*\s"
Set col = .Execute(sourceStr)
End With
Idx = 0
length = 0
For Each match In col
ReDim Preserve str(Idx)
str(Idx) = match.Value
length = length + match.length
Idx = Idx + 1
Next
'get the last string
If length <> Len(sourceStr) Then
ReDim Preserve str(Idx)
str(Idx) = Mid(sourceStr, length) & Chr(32)
End If
For Idx = UBound(str) To 0 Step -1
result = result & str(Idx)
Next
If result <> "" Then MsgBox result
Set regex = Nothing
End Function
-
Nov 14th, 2000, 03:37 PM
#5
If I'm not correct, shouldn't it be StrReverse rather than ReverseStr?
-
Nov 14th, 2000, 07:39 PM
#6
PowerPoster
Ya, Magatron you're rite the function in VB is StrReverse, Sorry for my typing mistake.
-
Nov 14th, 2000, 08:44 PM
#7
Similar to the one above, but a bit different:
Code:
Private Sub Command1_Click()
Dim vArray As Variant
Dim strArray As String
Dim iArray As Integer
vArray = Split("This is some text", " ")
For iArray = 0 To UBound(vArray)
strArray = strArray & Chr(32) & StrReverse(vArray(iArray))
Next iArray
MsgBox StrReverse(strArray)
End Sub
-
Nov 14th, 2000, 10:28 PM
#8
Thread Starter
Junior Member
Will any of these codings work when the string is taken from a user-entered text box? I need to make it work so that if a user enters some text in a text box and then clicks on a reverse line command button it reverses the string. Should any of the above codings be modified? Thanks in advance.
-
Nov 14th, 2000, 10:45 PM
#9
All code above will work, just find where it is used and change it to the textbox (Text1).
Code:
Private Sub Command1_Click()
Dim vArray As Variant
Dim strArray As String
Dim iArray As Integer
vArray = Split(Text1.text, " ")
For iArray = 0 To UBound(vArray)
strArray = strArray & Chr(32) & StrReverse(vArray(iArray))
Next iArray
MsgBox StrReverse(strArray)
End Sub
Or:
Code:
Private Sub Command1_Click()
Dim x As Integer
Dim intLow As Integer
Dim intHigh As Integer
Dim strInput As String
Dim strTest() As String
Dim strNew As String
strInput = Text1.text
strTest = Split(strInput)
intLow = LBound(strTest)
intHigh = UBound(strTest)
For x = intHigh To intLow Step -1
strNew = strNew & strTest(x) & " "
Next x
MsgBox "Original String = " & strInput & vbCrLf & _
"Reverse String = " & strNew
End Sub
Or, I guess sourceStr would be Text1.text:
Code:
Private Function ReverseStr(ByVal sourceStr As String) As String
Dim regex As RegExp
Dim col As MatchCollection
Dim match As match
Dim str() As String
Dim result As String
Dim length As Long
Dim Idx As Integer
'***************************************************************
' This example code need the VBScript.dll file
' Before you use this code make sure you have
' this DLL in your Windows system folder.
'
' You can download this DLL from the following URL
'http://www.microsoft.com/msdownload/vbscript/scripting51.asp
'
'***************************************************************
Set regex = New RegExp
With regex
.IgnoreCase = True
.Global = True
'\S* search for all non white space charater
'\s search for all space character
.Pattern = "\S*\s"
Set col = .Execute(sourceStr)
End With
Idx = 0
length = 0
For Each match In col
ReDim Preserve str(Idx)
str(Idx) = match.Value
length = length + match.length
Idx = Idx + 1
Next
'get the last string
If length <> Len(sourceStr) Then
ReDim Preserve str(Idx)
str(Idx) = Mid(sourceStr, length) & Chr(32)
End If
For Idx = UBound(str) To 0 Step -1
result = result & str(Idx)
Next
If result <> "" Then MsgBox result
Set regex = Nothing
End Function
-
Nov 15th, 2000, 12:23 AM
#10
Addicted Member
Hi,
Opssss.......Sorry !!!!!! I forgot The Function.
But With All GREAT samples that u have now - i dont think u need my poor sample - But here it is.
MyReversText.text = ReverseString(Text1.Text)
Private Function ReverseString(strSource As String) As String
'
Dim pos As Integer 'position
Dim strDummy As String 'dummy string
Dim intC As Integer 'counter
Const Space As String = " " 'space
'
pos = Len(strSource)
strDummy = ""
For intC = Len(strSource) To 1 Step -1
If Mid(strSource, intC, 1) = Space Then
strDummy = strDummy & Mid(strSource, intC, pos - intC + 1)
pos = intC
End If
Next intC
strDummy = strDummy & Left(strSource, pos - intC)
ReverseString = Right(strDummy, Len(strDummy) - 1)
'
End Function
Regards
-
Nov 15th, 2000, 12:37 AM
#11
transcendental analytic
Might be handy if you want some speed 
Code:
Function fastStrReverse(text As String) As String
Dim a() As Byte, b() As Byte, c As Long, n As Long
a = StrConv(text, vbFromUnicode)
c = UBound(a)
ReDim b(c)
For n = 0 To c
b(c - n) = a(n)
Next n
fastStrReverse = StrConv(b, vbUnicode)
End Function
Fast as you can get if you have vb5, but in vb6 the built in is probably faster
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|