|
-
Jun 30th, 2005, 10:48 AM
#1
Thread Starter
Addicted Member
String split (Resolved)
Hey All,
How do you split the following text from a text box...
MyText, andsomemoretext
...and put the second part into text2 textbox?
I'm using VB5, and cannot use the Split function.
Thanks in advance,
Ron
Last edited by rdcody; Jun 30th, 2005 at 12:20 PM.
-
Jun 30th, 2005, 10:58 AM
#2
Re: String split
Use the InStr function to check for the instance of the dilimiter.
Then, is InStr returns greater than 0 use the Left$() or Right$() string functions to get all the text before (or after depending on which one you use).
And that should do it 
Cheers,
RyanJ
-
Jun 30th, 2005, 11:00 AM
#3
Fanatic Member
Re: String split
try something like this.
VB Code:
Private Sub Command1_Click()
Dim strFirstString As String
strFirstString = "MyText, andsomemoretext"
Dim x As String
Dim y As String
x = Left$(strFirstString, InStr(1, strFirstString, ",") - 1)
y = Trim$(Mid$(strFirstString, InStr(1, strFirstString, ",") + 1, _
(Len(strFirstString) - (InStr(1, strFirstString, ",")))))
text1.Text = x
text2.Text = y
End Sub
Hope this helps!!
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
Jun 30th, 2005, 11:09 AM
#4
Re: String split
You can use the split command like tihs:
VB Code:
Text2.Text = Split(Text1, ",")(1)
Text1.Text = Split(Text1, ",")(0)
Pradeep 
EDIT: The above code is assuming that your Text1 contains the string you want to split.
-
Jun 30th, 2005, 11:15 AM
#5
Re: String split
One more way (using split and control arrays).
-Place a textbox on the form, name it Text1, set its index property to 0.
-Place another texbox and name it Text2.
-Place a command button and name it Command1.
-Paste the code in the form
-Run the application
-Enter the text (delimited with commas) in Text2
-Press the command button
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim strBuffer() As String
Dim I As Long
strBuffer = Split(Text2.Text, ",")
For I = LBound(strBuffer) To UBound(strBuffer)
If I <> 0 Then
Load Text1(I)
Text1(I).Text = Trim$(strBuffer(I))
Text1(I).Top = Text1(I - 1).Top + Text1(I).Height
Text1(I).Visible = True
Else
Text1(I).Text = Trim$(strBuffer(I))
End If
Next I
End Sub
-
Jun 30th, 2005, 12:19 PM
#6
Thread Starter
Addicted Member
Re: String split
Thanks for the replies fellers.
The solution that Bombdrop provided worked very well. The input of
sciguyryan explained it to me, and the solutions of baja_yu, and Pradeep1210
use the Split function which I can't use because I'm using VB5.
Thanks to all of you for your help!
-
Jun 30th, 2005, 12:38 PM
#7
Re: String split (Resolved)
Here is one that does it without the split function so you can use it. It works with more than two items.
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim strBuffer As String
Dim intPass As Long
intPass = 0
Do
If InStr(Text2.Text, ",") > 0 Then
strBuffer = Left$(Text2.Text, InStr(Text2.Text, ",") - 1)
Text2.Text = Right$(Text2.Text, Len(Text2.Text) - InStr(Text2.Text, ","))
Else
strBuffer = Text2.Text
Text2.Text = ""
End If
If intPass <> 0 Then
Load Text1(intPass)
Text1(intPass).Text = Trim$(strBuffer)
Text1(intPass).Top = Text1(intPass - 1).Top + Text1(intPass).Height
Text1(intPass).Visible = True
Else
Text1(intPass).Text = Trim$(strBuffer)
End If
intPass = intPass + 1
Loop Until InStr(Text2.Text, ",") = 0
If Len(Text2.Text) > 0 Then
Load Text1(intPass)
Text1(intPass).Text = Trim$(Text2.Text)
Text1(intPass).Top = Text1(intPass).Top + Text1(intPass).Height
Text1(intPass).Visible = True
Text2.Text = ""
End If
End Sub
-
Jul 4th, 2005, 03:23 PM
#8
Lively Member
Re: String split (Resolved)
-
Jul 4th, 2005, 06:34 PM
#9
Re: String split (Resolved)
There is a Split function for VB5 in Codebank, and also on MSDN Knowledge Base there is a page with Split and Replace functions for VB5.
Here's one by abhijit.
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
|