|
-
Jul 27th, 2006, 08:16 AM
#1
Thread Starter
Fanatic Member
about split function
hi, im trying to split some text file from Text1.Text (Multiline)
from a text1.text it appears like this
Location 1
CENTER: 12345
REMOTE: +1234
MESSAGE: OKAY
what im trying to do is get the REMOTE: so it will appear on my text1.text
because what i did is the Text1.text(multiline) = false
and my code
VB Code:
text1.text = split(text1.text,"+",,vbTextCompare) (1)
-
Jul 27th, 2006, 08:26 AM
#2
Lively Member
Re: about split function
Ummm... I don't get it at all. Could you be a bit clearer about what it is your doing and what your problem is?
-
Jul 27th, 2006, 08:50 AM
#3
Re: about split function
VB Code:
Const TEST_DATA = "REMOTE: +1234"
Dim strParts() As String
strParts = Split(TEST_DATA, "+")
Text1.Text = strParts(1)
-
Jul 27th, 2006, 09:01 AM
#4
Re: about split function
I hate SPLIT() - it's got a purpose...
but it's abused...
SPLIT is for taking something that has regular delimiters - maybe commas or slashes or CARRIAGE RETURNS - and splitting that line into an array.
It certainly is not for finding a value after the word REMOTE: that happens to have a + in front of it.
Use INSTR - find the position of the word REMOTE:
Then use INSTR again to find the CRLF that follows the value just found.
Then use MID to cut out the piece after the word REMOTE: but only up to the CRLF.
-
Jul 27th, 2006, 09:07 AM
#5
Re: about split function
Try something like this instead...
VB Code:
Option Explicit
Public i As Long
Public j As Long
Public txt As String
Public char As String
Public searched As String
Public result As String
Private Sub Command1_Click()
txt = Text1.text
searched = "REMOTE: "
result = ""
For i = 1 To Len(txt)
char = Mid(txt, i, 1)
If char = Left(searched, 1) Then
If Mid(txt, i, Len(searched)) = searched Then
For j = i + Len(searched) To Len(txt)
char = Mid(txt, j, 1)
If char = vbLf Then
MsgBox result
Exit Sub
Else
result = result & Mid(txt, j, 1)
End If
Next j
End If
End If
Next i
MsgBox "Nothing found!"
End Sub
It works for me
-
Jul 27th, 2006, 09:07 AM
#6
Fanatic Member
Re: about split function
I completely agree with szlamany.
Here's much simpler code that you can use
VB Code:
Dim strtPt As Long
Dim endPt As Long
strtPt = InStr(Text1.Text, "+") + 1
endPt = InStr(strtPt, Text1.Text, vbNewLine) - strtPt
Debug.Print Mid$(Text1.Text, strtPt, endPt)
-
Jul 27th, 2006, 09:08 AM
#7
Re: about split function
 Originally Posted by szlamany
I...It certainly is not for finding a value after the word REMOTE: that happens to have a + in front of it.....
Why on Earth not?
-
Jul 27th, 2006, 09:11 AM
#8
Re: about split function
He's got a multi-line textbox with the word REMOTE: sitting on one of the lines.
He wants the value after the word REMOTE: - up to the next CRLF.
The correct method is exactly what shirazamod posted.
Using SPLIT() to find this involved creating an array - not knowing what line in the array the REMOTE: item was involved with (remember it's a multi-line textbox). Plus SPLIT() is basically doing what shirazamod posted internally but repeatedly for creating an array.
SPLIT() has it's place but it's certainly not for this one (in my opinion )
-
Jul 27th, 2006, 10:36 AM
#9
Re: about split function
 Originally Posted by szlamany
He's got a multi-line textbox with the word REMOTE: sitting on one of the lines.
He wants the value after the word REMOTE: - up to the next CRLF.
The correct method is exactly what shirazamod posted.
Using SPLIT() to find this involved creating an array - not knowing what line in the array the REMOTE: item was involved with (remember it's a multi-line textbox). Plus SPLIT() is basically doing what shirazamod posted internally but repeatedly for creating an array.
SPLIT() has it's place but it's certainly not for this one (in my opinion  )
You're right (you both are). I missed the fact that the source is a multi-line textbox.
-
Jul 27th, 2006, 11:53 AM
#10
Addicted Member
Re: about split function
Assuming Text1.Text has..
VB Code:
Location 1
CENTER: 12345
REMOTE: +1234
MESSAGE: OKAY
Then something along the lines of..
VB Code:
Dim posNum As Integer
Dim spaceNum As Integer
Dim newStr as String
posNum = Instr(1, Text1.Text, "+", vbTextCompare)
spaceNum = Instr(posNum, Text1.Text, " ", vbTextCompare)
newStr = Mid(Text1.Text, posNum, spaceNum - 1)
newStr will equal +1234
- If you found my post to be helpful, please rate me.

-
Jul 27th, 2006, 12:46 PM
#11
Fanatic Member
Re: about split function
Yeah, that's pretty much what I said
-
Jul 27th, 2006, 07:12 PM
#12
Frenzied Member
Re: about split function
 Originally Posted by szlamany
The correct method is exactly what shirazamod posted.
His method looks for a +.
Therefore it would work only if the value of remote always begins with a + and no other value ever begins with a +.
If both of these conditions are true then the method works.
If not then it would be better to look for "REMOTE:", retrieve the rest of the line and trim away the spaces.
This also adds the benefit of being able to use the same code to retrieve the values of other fields.
You would only need to change the searchstring.
-
Jul 28th, 2006, 12:34 AM
#13
Thread Starter
Fanatic Member
Re: about split function
hi, thanks for the reply i test the code it works fine but im having problem
this is the actual text what im reading
Location 1, folder "Inbox", SIM memory, Inbox folder
SMS message
SMSC number : "+639170000119"
Sent : Friday, July 28, 2006 1:35:52 PM +0800
Coding : Default GSM alphabet (no compression)
Remote number : "+639150001234"
Status : UnRead
Hello World
i want the +639170000119 appear on Text1.text and +639150001234 appear in Text2.text
-
Jul 28th, 2006, 01:52 AM
#14
Fanatic Member
Re: about split function
Then just change the code to look for SMSC number : and then to look for Remote number : and then use Replace() to remove Chr(34)
-
Jul 28th, 2006, 01:55 AM
#15
Thread Starter
Fanatic Member
Re: about split function
@shirazamod
Sir, what do you mean?
-
Jul 28th, 2006, 01:59 AM
#16
PowerPoster
Re: about split function
VB Code:
Private Sub Command1_Click()
Dim sLine() As String, sTitle As String, sValue As String
Dim x As Integer, y As Integer, z As Integer
sLine = Split(Text1, vbCrLf)
For x = 0 To UBound(sLine)
y = InStr(sLine(x), ":")
z = InStrRev(sLine(x), ":")
If y Then
sTitle = Trim$(Left$(sLine(x), y - 1))
sValue = Right$(sLine(x), Len(sLine(x)) - z)
sValue = Trim$(Replace(sValue, """", ""))
If sTitle = "SMSC number" Then
Text1 = sValue
ElseIf sTitle = "Remote number" Then
Text2 = sValue
End If
End If
Next x
End Sub
OR ..
VB Code:
Private Sub Command1_Click()
Dim sLine() As String, x As Integer
Dim sTitle As String, sValue As String
sLine = Split(Text1, vbCrLf)
For x = 0 To UBound(sLine)
Call GetLine(sLine(x), ":", sTitle, sValue)
If sTitle = "SMSC number" Then
Text1 = sValue
ElseIf sTitle = "Remote number" Then
Text2 = sValue
End If
Next x
End Sub
Private Sub GetLine(ByVal str As String, ByVal val As String, _
ByRef sTitle As String, ByRef sValue As String)
Dim y As Integer, z As Integer
y = InStr(1, str, val, 3): z = InStrRev(str, val, , 3)
If y Then
sTitle = Trim$(Left$(str, y - 1))
sValue = Right$(str, Len(str) - z)
sValue = Trim$(Replace(sValue, """", ""))
End If
End Sub
OR ..
VB Code:
Private Sub Command1_Click()
Dim sLine() As String, x As Integer
Dim sTitle As String, sValue As String
sLine = Split(Text1, vbCrLf)
For x = 0 To UBound(sLine)
sTitle = GetTitle(sLine(x), ":")
sValue = GetValue(sLine(x), ":")
If sTitle = "SMSC number" Then
Text1 = sValue
ElseIf sTitle = "Remote number" Then
Text2 = sValue
End If
Next x
End Sub
Private Function GetTitle(ByVal str As String, ByVal val As String) As String
Dim i As Integer: i = InStr(1, str, val, vbTextCompare)
If i Then GetTitle = Trim$(Left$(str, i - 1))
End Function
Private Function GetValue(ByVal str As String, ByVal val As String) As String
Dim i As Integer: i = InStrRev(str, val, , vbTextCompare)
If i Then GetValue = Trim$(Replace(Right$(str, Len(str) - i), """", ""))
End Function
Last edited by rory; Jul 28th, 2006 at 02:32 AM.
-
Jul 28th, 2006, 02:18 AM
#17
Fanatic Member
Re: about split function
Or even simpler
VB Code:
Dim strtPt As Long
Dim endPt As Long
strtPt = InStr(Text1.Text, "SMSC number : ") + 14
endPt = InStr(strtPt, Text1.Text, vbNewLine) - strtPt
Text1 = Replace$(Mid$(Text1.Text, strtPt, endPt), Chr(34), vbNullString)
strtPt = InStr(Text1.Text, "Remote number : ") + 16
endPt = InStr(strtPt, Text1.Text, vbNewLine) - strtPt
Text2 = Replace$(Mid$(Text1.Text, strtPt, endPt), Chr(34), vbNullString)
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
|