Solved

vb string parsing help needed

Posted on 2011-09-23
5
207 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
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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

22 Experts available now in Live!

Get 1:1 Help Now