|
-
Aug 17th, 2007, 10:07 AM
#1
Thread Starter
Fanatic Member
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.
-
Aug 17th, 2007, 10:26 AM
#2
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
-
Aug 17th, 2007, 10:37 AM
#3
Thread Starter
Fanatic Member
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!
-
Aug 17th, 2007, 10:41 AM
#4
Thread Starter
Fanatic Member
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?
-
Aug 17th, 2007, 12:55 PM
#5
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
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
|