Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb string parsing help needed

Posted on 2011-09-23
5
Medium Priority
?
244 Views
Last Modified: 2012-05-12
I have a file of names that I need to parse out into a shorter version of the name as I write them to a new file.  In each record, the string psOrig contains the company name, and it might be one, two, or three words or more, with spaces in between each word.  For example, psOrig might contain 'Sony', or 'Sony Electronics', or 'Sony Electronics Corporation', etc.  I need a way to put only the first two words into psNew, meaning that if psOrig contains 'Sony Electonics', or 'Sony Electronics Corporation', all that would end up in psNew would be 'Sony Electronics'.  And, if psOrig only contains one word, then that's what I would want in psNew.

I need help with coding this, with a routine that can detect the spaces and parse this out properly for each name.....TIA
 
0
Comment
Question by:sasllc
  • 2
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 1000 total points
ID: 36590650
Dim psOrig as String = "Sony Electronics Corp"
Dim parts as String() = psOrig.Split(New Char() {" "c})
Dim psNew as String = parts(0) + " " + parts(1)
0
 
LVL 3

Author Comment

by:sasllc
ID: 36590839
This works great--except for the situation where psOrig was a single word name, such as 'Sony'.  In that case I get this error: 'Index was outside the bounds of the array.'  Any good solution to this problem?
0
 
LVL 40
ID: 36590948
Dim psOrig as String = "Sony Electronics Corp"
Dim parts as String() = psOrig.Split(New Char() {" "c})
Dim psNew As String

If parts.Count = 2 Then
   psNew = parts(0) + " " + parts(1)
Else
   psNew = parts(0)
end if
 
0
 
LVL 3

Author Comment

by:sasllc
ID: 36590978
When I use this syntax, I get this error: 'count is not a member of System.Array'.  As I typed in the command, I only had seven options, and 'count' was not one of them.  I wonder if maybe this is because I'm working in the limited 'smart device' environment, as I develop this program for a scanner.  The options I show are:

clone
get length
get lower bound
get upper bound
length
rank
SetValue

Any workaround in this environment?
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 1000 total points
ID: 36591011
Sorry, I mixed arrays and collections (they are very similar), and I use collections a lot more often.

Use Length instead of Count.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question