CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Q: Split("")

  1. #1
    Join Date
    Jan 2009
    Posts
    3

    Q: Split("")

    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

    ------------------

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Q: Split("")

    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.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Q: Split("")

    I haven't seen PEEK() used for a long time. Where did you find that?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Q: Split("")

    Quote Originally Posted by dglienna View Post
    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....)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jan 2009
    Posts
    3

    Re: Q: Split("")

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Q: Split("")

    Mark it RESOLVED. (and post a CODE SAMPLE for the next visitor)
    Code:
    ' Always use CODE TAGS
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured