Solved

vb string parsing help needed

Posted on 2011-09-23
5
195 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 250 total points
Comment Utility
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
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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 250 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now