Find a string in VB.NET

  • Thread starter Federico G. Babelis
  • Start date
F

Federico G. Babelis

Hi,

I need to extract a string from another string separated by "," like a .csv
file.
for example I have this string:

String1 = 000,federico,00,439827HGH,1233,FGB,0000,00,000

and from that string i need to extract string "federico" and string "FGB"
and separate then into another 2 strings:

String2 = "federico"
String3 = "FGB"

How this could be done in Visual Basic .NET ????

Thanks and best regards,
Federico
 
W

What-a-Tool

Dim Delimiter() As Char = {ToChar(",")}

pstrFields = String1.Split(Delimiter)



Returns an array containing each seperate string


--

/ Sean the Mc /


"I have not failed. I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)
 
F

Federico G. Babelis

Works fine thanks !!!!



What-a-Tool said:
Dim Delimiter() As Char = {ToChar(",")}

pstrFields = String1.Split(Delimiter)



Returns an array containing each seperate string


--

/ Sean the Mc /


"I have not failed. I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)
 
J

Jonathan Yong

The second option is to use Regular Expression.
My take is if you have a lot of similar string to process(EG : you read
from a CSV file), creating array of string of each of your input string
is going to be very expensive.
 
J

Justin Rogers

What an array of strings is going to have a higher commit charge than all of
the associated objects inside of a regular expression match? Just an FYI,
every Match holds a bunch of groups and then each group a bunch of
captures. Each capture has an index, length, and associated string reference.
Each group has all of the above, plus an integer array of captures, a capture
count, and a CaptureCollection. Each Match in turn has everything of the
above, and a GroupCollection, a pointer to the regex, a bunch of integer
fields for offsets, a jagged two-dimensional array, and a single dimensional
array. Can you honestly tell me that the Regex is going to be less expensive?

Long story short, your best bet is still going to be string.split. If you are
worried
about the multiple strings being created, then you can use a customized version
of the algorithm that only allocates strings for the indices you are going to
need.
I have a Split algorithm that does a split by substring, rather than character
array
that you might find useful and could be modified to only return the strings you
are interested in.

http://weblogs.asp.net/justin_rogers/archive/2004/03/14/89545.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top