Results 1 to 5 of 5

Thread: Trim strings

  1. #1

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    Resolved Trim strings

    I need to read in a txt file into different combo boxes...

    Depending on what the first words are will depend on which combo box it's placed into...

    so, for example, my file (called settings.txt) will be:

    site=USA
    site=Canada
    site=Mexico
    phone=home
    phone=cell
    phone=office

    so, I know I'll have to use a Streamreader and loop through the text, but how do I get it so that it trims off everything before the ='s sign?

    so, my streamreader should read the following and add them to the

    site combobox:
    USA
    CANADA
    Mexico

    phone combobox:
    home
    cell
    office

    Any ideas?
    I know I'll need to loop through and use TRIM but I'm not sure ...
    Last edited by mateo107; Aug 17th, 2007 at 11:02 AM.


    -Matthew-

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Trim strings

    you can use IO.File.ReadAllLines to read the lines in the file into a string array. You can then loop through the array and break down the lines using substring and place them in the appropriate combobox. Something like this...
    Code:
            Dim Lines As String() = IO.File.ReadAllLines("PATH_TO_FILE")
            For Each line As String In Lines
                Select Case line.Substring(0, line.IndexOf("="))
                    Case "site"
                        Me.SiteCombo.Items.Add(line.Substring(line.IndexOf("=") + 1))
                    Case "phone"
                        Me.PhoneCombo.Items.Add(line.Substring(line.IndexOf("=") + 1))
                End Select
            Next
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    Re: [2005] Trim strings

    your code works like a champ!

    JUST what I needed!

    I love being able to come here, learn something new and get helped by all the friendly other .NETTERs out there..

    being a newbie, and not a terribly "logical" person, I have trouble using all of .NETs tricks unless I have examples!

    Thanks a millioN!


    -Matthew-

  4. #4

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    Re: [RESOLVED] [2005] Trim strings

    Actually,

    if the line is blank (hard enter) it errors out because it encounters a "null" line essentially... and throws an error... how can i get it to ignore blanks?


    -Matthew-

  5. #5
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Trim strings

    just add a check for an empty line

    Code:
            Dim Lines As String() = IO.File.ReadAllLines("PATH_TO_FILE")
            For Each line As String In Lines
                If line.Trim <> String.Empty Then
                    Select Case line.Substring(0, line.IndexOf("="))
                        Case "site"
                            Me.SiteCombo.Items.Add(line.Substring(line.IndexOf("=") + 1))
                        Case "phone"
                            Me.PhoneCombo.Items.Add(line.Substring(line.IndexOf("=") + 1))
                    End Select
                End If
            Next
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

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