Click to See Complete Forum and Search --> : Q: Split("")
ArtemisX
January 9th, 2009, 06:42 AM
Just starting to get my head around VB and i've managed to get a small program to open a text file, read the first line and split each word in the sentence into several different text boxes on a GUI. i press a button on my GUI and it opens the text file, splits the line into each word and drops it into text boxes, code i use is below. instead of splitting by a space (as in this example) is it possible in a similar way to split by particular words in that sentence instead? i noticed somewhere that i could use LineOfText.Split(" "c) where the "c" indicates a character but W for word or anything else doesn't work.
Any pointers would be apprechiated!
------------
Dim FILE_NAME As String = "C:\text2.txt"
Dim TextLine As String
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & vbNewLine
Loop
TextBox1.Text = TextLine
Dim LineOfText As String
Dim aryTextFile() As String
LineOfText = TextLine
aryTextFile = LineOfText.Split(" ")
TextBox1.Text = aryTextFile(0)
TextBox2.Text = aryTextFile(1)
Else
MsgBox("File Does Not Exist")
End If
------------------
TheCPUWizard
January 9th, 2009, 06:57 AM
1) Please RE-READ the "BEFORE you post announcement" and then go back and edit your existing post to properly use code tags
2) Please enable Private messaging as indicated in the same document
Now to the "problem".
String.Split allows for splitting based on a character being present in the source string OR a string being in the source string.
Unfortunately, VB uses the double-quote ("") syntax for literals of both types (and defaults to string),so (" "c) is telling the compiler to split based on the character "space", while omiting the c will cause the compiler to geterate code that willsplit the string based on it containging a string that is a single character.
WHY does this matter??? A character is a value type, a string is an immutable reference type.
This should be covered in what ever book you are carefully following (no skipping, etc) and studying (this means typing in all of the code, stepping through every line to understand what actually happens when each line is executed).
Programming does not lend itself to "ad-hoc" random learning. There are simply too many details (like the one above) which if not introduced at the proper time [a book should have covered this in the topic on types, and file/stream operations would be a significant number of chapters later]
Statistically, people who try to learn from multiple web-sites and/or other non-structured sources end up in one of the two following categories:
1) They have so many fundamental misunderstandings, that they are never able to proceed to writing proper "correct" programs (Remember a program can compile, run, produce the desired output) and STILL not be correct.
2) Get so fustrated that they give up, and never program again.
The best way to avoid this, is to invest in a good book, and spend a few hundred hours (most people I have seen over the past 25+ years take 160-320 hours to gain a basic understanding in a language such as this).
AFTER completing this, then it will be much easier to write programs such as the one you are attempting now.
dglienna
January 10th, 2009, 02:01 AM
I haven't seen PEEK() used for a long time. Where did you find that?
TheCPUWizard
January 10th, 2009, 02:05 AM
I haven't seen PEEK() used for a long time. Where did you find that?
Maybe he "POKE"'ed around???
(sorry, that was just too easy, especially at 3:00AM Local....)
ArtemisX
January 10th, 2009, 06:26 PM
for sense of completion i did find a solution of sorts. instead of messing about i just replaced the words i knew would be in the text with a character that split() could recognise and do it that way.
Seemed easier than reading and learning for 25 years - after all its only my first VB program.
dglienna
January 10th, 2009, 08:42 PM
Mark it RESOLVED. (and post a CODE SAMPLE for the next visitor)
' Always use CODE TAGS
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.