Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > parsing through a string


Reply
 
Thread Tools Display Modes
  #1  
Old 07-22-2012, 11:00 PM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Question parsing through a string


I need to assign a value to a variable by getting it from a text file. The value I need starts in column 15 and has an undetermined length.. I just know the value is followed by a blank so I would have to read numbers until I hit a blank. I need to load the variable into a list that I'm going to later compare against another table that I created. Does anyone know how I can do this?

TIA
Joni
Reply With Quote
  #2  
Old 07-23-2012, 05:44 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Read in the entire line. Use String.IndexOf() to find the index of the first blank. Then use String.Substring() to get the part of the string before the blank. Then use your method of choice to convert that substring into numbers. If you have any questions about those methods, check the documentation, then post what you've tried if that doesn't answer the question.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 07-23-2012, 07:07 AM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Smile

Thanks but I'm not sure that's going to work. Reason being that there are blanks randomly throughout the record, both before and after the field I need to pull. Is there a function that will start in a set location (column 15) and read the characters 1 by 1 until a specific character is hit (in this case, a blank)?
Reply With Quote
  #4  
Old 07-23-2012, 07:20 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Yes, if you look at the documentation for String.IndexOf() you'll find there's an overload that lets you tell it where to start looking.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 07-23-2012, 08:23 AM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Default

Thanks so much... I'll give it a try
Reply With Quote
  #6  
Old 07-23-2012, 09:13 AM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Default

I got it to work, but tried it a different way... probably cheated a bit but it works

Rpt_TotalRecs = Mid(Rpt_REC, 15, 10)
Rpt_TotalRecs = Rpt_TotalRecs.Trim

Thanks for your help.. it's very appreciated!
Joni
Reply With Quote
  #7  
Old 07-24-2012, 08:00 AM
nosheep nosheep is offline
Newcomer
 
Join Date: Oct 2008
Location: United Kingdom
Posts: 2
Default fixed length!!

Of course, passing a length of 10 into Mid won't help you if you have a field of indetermined length as you indicated in your OP. For this, you'd have to use Instr() to find the first occurrence of a space after position 15 and subtract 15 from this position.

Rpt_TotalRecs = Mid(Rpt_REC, 15, instr( Rpt_REC, 15, " ") -15)

This should return the string from position 15 to the position before the next occurring space char.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->