treat it the same as a normal array.. an array list has a little more functionality than an array.
StringArray(0).. StringArray(1)
http://msdn2.microsoft.com/en-us/lib...arraylist.aspx
Printable View
treat it the same as a normal array.. an array list has a little more functionality than an array.
StringArray(0).. StringArray(1)
http://msdn2.microsoft.com/en-us/lib...arraylist.aspx
I have to split an array by line, how can i do this, What is the enter character that i should do a string.split with?
Environment.NewLine ;)
Code:Dim splstr() As String
splstr = TextBox1.Text.Split(vbCrLf)
Chr(13) might work as well.
since this is .net Environment.Newline is the correct method as Athiest posted. vbCrLf is legacy code from vb6 and is only preserved for updating legacy applications to .net.
what would code look like for environment.newline
VB Code:
Dim StringArray() As String = SomeString.Split(Environment.NewLine)
An array is usually already split. The only way the split command would work is if each element of the array had more than one line in it, and then you'd have to apply the command to each element, one by one.Quote:
Originally Posted by ethanhayon
Split is used to split a single string into an array, not an array into strings.
atheist, with your code, how would i read a certain line, like, what the code look like to view the line and not only break it up
What do you mean? Read from a textfile?
no, you showed me how to break it up.. So how would i view line 7 (for example)
vb.net Code:
MsgBox(StringArray(6)) ' to view in a message box
yea, i tried that, if i do any number larger than 0, i get a null value
Any ideas???
What is the string that you are trying to split up ?
Code:[ 5], AP BSS 5.300 ( 60) 90 00:14:a5:0e:ce:04 nw
[ 5], AP BSS 5.300 ( 60) 90 00:14:a5:0e:ce:04 nw
[ 2], AP BSS 5.300 ( 60) 78 00:14:a5:0e:ce:04 nw
[ 3], AP BSS 5.300 ( 60) 27 00:14:a5:0e:ce:38 nw
[ 6], AP BSS 5.300 ( 60) 27 00:14:a5:0e:ce:04 nw
[ 4], AP BSS 5.300 ( 60) 27 00:37:b4:0e:bc:62 nw
[ 6], AP BSS 5.300 ( 60) 27 00:14:a5:0e:ce:04 nw
[ 4], AP BSS 5.300 ( 60) 27 00:37:b4:0e:bc:62 nw
so you want..
etc..vb.net Code:
StringArray(0) = "[ 5], AP BSS 5.300 ( 60) 90 00:14:a5:0e:ce:04 nw" StringArray(1) = "[ 5], AP BSS 5.300 ( 60) 90 00:14:a5:0e:ce:04 nw" StringArray(2) = "[ 2], AP BSS 5.300 ( 60) 78 00:14:a5:0e:ce:04 nw"
yes, thats what i need to get
i did a little sample and got it to work, how are you receiving the string of text ?
i am using an algorithm to sort it into an array
can you copy the code so i have a better understanding of how its being done ?
what did you have to do to get it to work
I just put the strings into a text box, and then got the text from there...
vb.net Code:
Dim StringArray() As String = TextBox1.Text.Split(Chr(13)) ' This is where it splits it and gets the string... Dim s As String Dim i As Integer = 0 Label1.Text = "" For Each s In StringArray Label1.Text &= "StringArray[ " & i.ToString & " ] = " & StringArray(i) & Environment.NewLine i += 1 Next
that only seems to work for the first two lines, when i do 3 it gives me null values
How are you building the string..
can i see the code to build it ?
actually, it doesnt even work for 2 lines
Are you reading this text from a text file? If so, you can just call IO.File.ReadAllLines(textfilepath), and this will return a string array where each element is a line in your text file.
if its a text file we can just read it from that and store it in an array, or just split it up by .NewLine.. depends how he is building the string.Quote:
Originally Posted by stanav
i am in the beginning, but at this point it is already going to be converted
i have to go, maybe stanav will be able to help you out on this. i will be back tomorrow to see if it is resolved.
well, this is my code....
vb Code:
Option Strict On Imports System.IO Public Class Form1 Private Function CompareStrings(ByVal x As String, ByVal y As String) As Integer Dim xtest As String = x.Substring(x.IndexOf(")") + 5) Dim xtest2 As String = xtest.Substring(xtest.IndexOf("")) Dim strArray1() As String = xtest2.Split(" "c) Dim xtest3 As String = strArray1(0) Dim ytest As String = y.Substring(y.IndexOf(")") + 5) Dim ytest2 As String = ytest.Substring(ytest.IndexOf("")) Dim strArray2() As String = ytest2.Split(" "c) Dim ytest3 As string = strArray2(0) Dim numX As Integer = CInt(xtest3) Dim numY As Integer = CInt(ytest3) If Integer.TryParse(CStr((numX)), numX) AndAlso _ Integer.TryParse(CStr((numY)), numY) Then Return numY - numX Else MsgBox("Failed") Return 0 End If End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim nwline As String Dim strmReader As New StreamReader("C:\Users\Ethan Hayon\Desktop\bssoutput2.txt") Do While strmReader.Peek > -1 nwline = strmReader.ReadLine If nwline.Contains("nw") Then ListBox1.Visible = True ListBox1.Items.Add(nwline) Dim count As String = ListBox1.Items.Count.ToString() Dim Strings As New List(Of String) For Each itm As String In Me.ListBox1.Items Dim myWords As String = Nothing myWords &= itm & Environment.NewLine Strings.Add(itm) ' MsgBox(count) Next Strings.Sort(New Comparison(Of String)(AddressOf CompareStrings)) Dim nwlinestring As String = String.Join(Environment.NewLine, Strings.ToArray()) TextBox9.Text = CStr(nwlinestring) Dim l As Integer = 1 Do Until l = CDbl(count) Dim i As Integer = 0 Dim StringArray() As String = nwlinestring.Split(Chr(13)) TextBox5.Text = StringArray(i) & Environment.NewLine ' TextBox5.Text = nwlinestring l = l + 1 Loop End If Loop strmReader.Close() End Sub End Class
i am using strings.add to add it to a string
Okay, nevermind about that, now i just basically wrote a text file and i am going to re read it. How would i read a text file line by line
The RegexMan strikes back... this should work regardless where the newlines are (if you check out the string, one line has a newline, the others don't but it still pulls three results)... The pattern searches for something in brackets, and ending with either nw, se, sw, ne. Not sure if they are constant, directions, or some tech lingo that could change... beats me... so if they are not directions, feel free to harp on me for my tech stupidity... if they are always nw, then just remove the other options in the pattern... the whole point was to show the various regex syntaxi (new word created by me) that you can play with...
If you are wanting to get that portion of a line in an array, then just loop for each array item and replace the "test" variable with that array item... although if that is the case you can further reduce it to use the match method instead of using a matchcollection if there is only going to be one match per line...
P.S. Regex is King.vb Code:
Dim Test As String = _ "[ 5], AP BSS 5.300 ( 60) 90 00:14:a5:0e:ce:04 nw" & _ "[ 5], AP BSS 5.300 ( 60) 80 00:14:a5:0e:ce:04 nw" & Environment.NewLine & _ "[ 5], AP BSS 5.300 ( 60) 70 00:14:a5:0e:ce:04 se" Dim Matches As System.Text.RegularExpressions.MatchCollection = _ System.Text.RegularExpressions.Regex.Matches(Test, "\[.*?\].*?(nw|sw|se|ne)") For Each match As System.Text.RegularExpressions.Match In Matches MessageBox.Show(match.Value) Next
im not completely sure as to what you are trying to say, i cant figure out why splitting by character 13 didnt work. Anybody have any ideas cause it should have worked fine
He said first that he has these lines in an array, if thats the case, then there are no new line characters... he should just loop through the array and parse out the info... but then in later posts it changes, so who knows...
vb.net Code:
Dim SR as New StreamReader(fileName) Dim StringArray() as New ArrayList With SR While Peek <> -1 ' .Peek checks if there is another line.. StringArray.Add(.ReadLine) ' Adds the items to an ArrayList End While .Close() End With
then what would i do with the arrayList?
gigemboy, im not quite sure if i understand your code, i don know why you are using se and sw and ne????
He explained why right in the post:Quote:
Originally Posted by ethanhayon
Quote:
The pattern searches for something in brackets, and ending with either nw, se, sw, ne. Not sure if they are constant, directions, or some tech lingo that could change... beats me... so if they are not directions, feel free to harp on me for my tech stupidity... if they are always nw, then just remove the other options in the pattern