Solved

vb string parsing help needed

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modal Popup Extender control 1 22
HTML - Color not displaying correctly in EMAIL. 6 41
ASP/VB email question 4 35
Get number of Files in Directory and Sub Directories 2 42
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

831 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