473,472 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String array problem

Hi,

I'm trying to create an array containing the entire contents of a text file
and delimit the elements using vbCrLf.

The text file is like this....

-text1
-text2
-text3
etc...

I can read the file in like this...
Dim TEST As New StreamReader("C:\test.txt")
Dim line As String = TEST.ReadLine()
While Not line = Nothing
Console.WriteLine(line)
line = TEST.ReadLine()
End While

What i'm trying to do is load each element of the text file, in this case
each line (-text1, -text2, -text3 etc..) into an array which I can later use
to compare another string (let's say, strA) with and remove, if found, any
occurances of an array element from strA. So for example, if strA contains
"this is a string -line2" then the "-text2" part of is removed from strA
using strA = strA.Replace(Whatever match was found, "")

All I have at the moment is how to read the file in, as i wrote above, so
any help with the creation of the array and the comparing against another
string is much appreciated.

Cheers,
Paul
Nov 21 '05 #1
5 1214
Try something like:

Dim _sr As StreamReader = New StreamReader("C:\test.txt")

Dim _ss() As String = _sr.ReadToEnd().Replace(ControlChars.Lf,
String.Empty).Split(ControlChars.Cr)

_sr.Close()

For _i As Integer = 0 to _i.Length - 1
Dim _p As Integer = _strA.IndexOf(_ss(_i))
If p > -1 Then _strA = _strA.Remove(_p, _ss(_i).Length)
Next
"Paul" <me@home.com> wrote in message
news:fN********************@pipex.net...
Hi,

I'm trying to create an array containing the entire contents of a text
file and delimit the elements using vbCrLf.

The text file is like this....

-text1
-text2
-text3
etc...

I can read the file in like this...
Dim TEST As New StreamReader("C:\test.txt")
Dim line As String = TEST.ReadLine()
While Not line = Nothing
Console.WriteLine(line)
line = TEST.ReadLine()
End While

What i'm trying to do is load each element of the text file, in this case
each line (-text1, -text2, -text3 etc..) into an array which I can later
use to compare another string (let's say, strA) with and remove, if found,
any occurances of an array element from strA. So for example, if strA
contains "this is a string -line2" then the "-text2" part of is removed
from strA using strA = strA.Replace(Whatever match was found, "")

All I have at the moment is how to read the file in, as i wrote above, so
any help with the creation of the array and the comparing against another
string is much appreciated.

Cheers,
Paul

Nov 21 '05 #2
Hey, thanks for the reply Stephany.

I'm getting an error on your code on this line though.
For _i As Integer = 0 To _i.Length - 1
'Length' is not a member of 'Integer'

I changed that to _i.ToString.Length -1 and it compiled but only the matches
of the first line of the array are removed.
For example if strA contains -line1 then -line1 is removed but if strA
contains -line2 or anything else the match is not removed.
Thanks,
Paul
"Stephany Young" <noone@localhost> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Try something like:

Dim _sr As StreamReader = New StreamReader("C:\test.txt")

Dim _ss() As String = _sr.ReadToEnd().Replace(ControlChars.Lf,
String.Empty).Split(ControlChars.Cr)

_sr.Close()

For _i As Integer = 0 to _i.Length - 1
Dim _p As Integer = _strA.IndexOf(_ss(_i))
If p > -1 Then _strA = _strA.Remove(_p, _ss(_i).Length)
Next
"Paul" <me@home.com> wrote in message
news:fN********************@pipex.net...
Hi,

I'm trying to create an array containing the entire contents of a text
file and delimit the elements using vbCrLf.

The text file is like this....

-text1
-text2
-text3
etc...

I can read the file in like this...
Dim TEST As New StreamReader("C:\test.txt")
Dim line As String = TEST.ReadLine()
While Not line = Nothing
Console.WriteLine(line)
line = TEST.ReadLine()
End While

What i'm trying to do is load each element of the text file, in this case
each line (-text1, -text2, -text3 etc..) into an array which I can later
use to compare another string (let's say, strA) with and remove, if
found, any occurances of an array element from strA. So for example, if
strA contains "this is a string -line2" then the "-text2" part of is
removed from strA using strA = strA.Replace(Whatever match was found, "")

All I have at the moment is how to read the file in, as i wrote above,
so any help with the creation of the array and the comparing against
another string is much appreciated.

Cheers,
Paul


Nov 21 '05 #3
Should have been:

For _i As Integer = 0 To _ss.Length - 1
"Paul" <me@home.com> wrote in message
news:es********************@pipex.net...
Hey, thanks for the reply Stephany.

I'm getting an error on your code on this line though.
For _i As Integer = 0 To _i.Length - 1
'Length' is not a member of 'Integer'

I changed that to _i.ToString.Length -1 and it compiled but only the
matches of the first line of the array are removed.
For example if strA contains -line1 then -line1 is removed but if strA
contains -line2 or anything else the match is not removed.
Thanks,
Paul
"Stephany Young" <noone@localhost> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Try something like:

Dim _sr As StreamReader = New StreamReader("C:\test.txt")

Dim _ss() As String = _sr.ReadToEnd().Replace(ControlChars.Lf,
String.Empty).Split(ControlChars.Cr)

_sr.Close()

For _i As Integer = 0 to _i.Length - 1
Dim _p As Integer = _strA.IndexOf(_ss(_i))
If p > -1 Then _strA = _strA.Remove(_p, _ss(_i).Length)
Next
"Paul" <me@home.com> wrote in message
news:fN********************@pipex.net...
Hi,

I'm trying to create an array containing the entire contents of a text
file and delimit the elements using vbCrLf.

The text file is like this....

-text1
-text2
-text3
etc...

I can read the file in like this...
Dim TEST As New StreamReader("C:\test.txt")
Dim line As String = TEST.ReadLine()
While Not line = Nothing
Console.WriteLine(line)
line = TEST.ReadLine()
End While

What i'm trying to do is load each element of the text file, in this
case each line (-text1, -text2, -text3 etc..) into an array which I can
later use to compare another string (let's say, strA) with and remove,
if found, any occurances of an array element from strA. So for example,
if strA contains "this is a string -line2" then the "-text2" part of is
removed from strA using strA = strA.Replace(Whatever match was found,
"")

All I have at the moment is how to read the file in, as i wrote above,
so any help with the creation of the array and the comparing against
another string is much appreciated.

Cheers,
Paul



Nov 21 '05 #4
Thanks Stephany thats fixed it!

Cheers,
Paul

"Stephany Young" <noone@localhost> wrote in message
news:eC**************@TK2MSFTNGP12.phx.gbl...
Should have been:

For _i As Integer = 0 To _ss.Length - 1
"Paul" <me@home.com> wrote in message
news:es********************@pipex.net...
Hey, thanks for the reply Stephany.

I'm getting an error on your code on this line though.
For _i As Integer = 0 To _i.Length - 1
'Length' is not a member of 'Integer'

I changed that to _i.ToString.Length -1 and it compiled but only the
matches of the first line of the array are removed.
For example if strA contains -line1 then -line1 is removed but if strA
contains -line2 or anything else the match is not removed.
Thanks,
Paul
"Stephany Young" <noone@localhost> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Try something like:

Dim _sr As StreamReader = New StreamReader("C:\test.txt")

Dim _ss() As String = _sr.ReadToEnd().Replace(ControlChars.Lf,
String.Empty).Split(ControlChars.Cr)

_sr.Close()

For _i As Integer = 0 to _i.Length - 1
Dim _p As Integer = _strA.IndexOf(_ss(_i))
If p > -1 Then _strA = _strA.Remove(_p, _ss(_i).Length)
Next
"Paul" <me@home.com> wrote in message
news:fN********************@pipex.net...
Hi,

I'm trying to create an array containing the entire contents of a text
file and delimit the elements using vbCrLf.

The text file is like this....

-text1
-text2
-text3
etc...

I can read the file in like this...
Dim TEST As New StreamReader("C:\test.txt")
Dim line As String = TEST.ReadLine()
While Not line = Nothing
Console.WriteLine(line)
line = TEST.ReadLine()
End While

What i'm trying to do is load each element of the text file, in this
case each line (-text1, -text2, -text3 etc..) into an array which I can
later use to compare another string (let's say, strA) with and remove,
if found, any occurances of an array element from strA. So for
example, if strA contains "this is a string -line2" then the "-text2"
part of is removed from strA using strA = strA.Replace(Whatever match
was found, "")

All I have at the moment is how to read the file in, as i wrote above,
so any help with the creation of the array and the comparing against
another string is much appreciated.

Cheers,
Paul



Nov 21 '05 #5
"Paul" <me@home.com> wrote in message
news:fN********************@pipex.net...
I'm trying to create an array containing the entire contents of a text file and delimit the elements using vbCrLf.


Read the entire file into a String ([StreamReader].ReadToEnd),
then use

Imports Microsoft.VisualBasic
. . .
<array> = [Strings].Split( sFiledata, vbCrLf )

to break it up into lines.

Beware [String].Split(), which /only/ supports single-character delimiters.

HTH,
Phill W.
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10
by: Danny | last post by:
HI again Is there a nifty function in access that will: 1. return the amount of occurances of a small string within a larger string? this<br>is<br>a<br>test would return 3 for <br>
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
6
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
6
by: MackS | last post by:
Hello everyone I am faced with the following problem. For the first time I've asked myself "might this actually be easier to code in C rather than in python?", and I am not looking at device...
9
by: Ben | last post by:
Hello, I'm not a developper, so sorry if it's a stupid question... I'm trying to develop an application in vb.net and I have the following problem: I have some information in an array:...
4
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
12
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
18
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.