please help me. i need to take each string in each column of my datagridview

roivic lanoria ASRock 2.4 GHz Dual Core SSD 160 GB

and look like this in my datagridview

roivic lanoria|ASRock|2.4 GHz|Dual Core|SSD|160 GB

these strings are from a text file. help please.

please be more specific. What does each line in the text file look like?

Edited 3 Years Ago by tinstaafl

the text file only had this strings and they are all in one line like this

roivic lanoria ASRock 2.4 GHz Dual Core SSD 160 GB

The Split method will allow you to convert the line into a string array. You can split on any character but the default is a blank so if you start with

Dim line As String = "roivic lanoria ASRock 2.4 GHz Dual Core SSD 160 GB"

then do

Dim fields() As String = line.Split()

you get

fields(0) = "roivic"
fields(1) = "lanoria"
fields(2) = "ASRock"
fields(3) = "2.4"
fields(4) = "GHz"
fields(5) = "Dual"
fields(6) = "Core"
fields(7) = "SSD"
fields(8) = "160"
fields(0) = "GB"

thanks for the code. one more question. i want to check if those strings exist on the text file and if they exist then that string method will occur. what code will i need?

Edited 3 Years Ago by aarviii

One way is to read the whole file into memory. Then loop through each line to find the string you want

    Imports System.IO

    Dim lines() As String = File.ReadAllLines("myfile.txt")
    Dim fields() As String
    Dim found As Boolean = False
    For I=0 To lines.Count-1
        If line(I).Contains("roivic")
            found = True
            fields = lines(I).Split()
            Exit For
        End If
    Next

If your file is very large it might be better to read the file one line at a time to find the line you want

    Imports System.IO

    Dim sr As StreamReader = New StreamReader("myfile.txt")
    Dim fields() As String
    Dim found As Boolean = False
    Do While sr.Peek() >= 0
        line = sr.ReadLine()
        If line(I).Contains("roivic")
            found = True
            fields = lines(I).Split()
            sr.Close
            Exit Do
        End If
    Loop
    sr.Close()  

Edited 3 Years Ago by tinstaafl

thanks. can you please give a little explanation how's that For I=0 To lines.Count-1 and peek <> 1function i really get confused how it works.please

For I=0 To lines.Count-1

A For Loop is a loop with a known start and a known end. In this case we want to go from the first item of the array to the last item of the array. The confusing part is that arrays by default count from 0 so that means the last one will be the total number of items(lines.Count) minus 1(i.e. 10 items will be numbered from 0 to 9).

peek

The peek just looks ahead in the file and if there is a character there returns it's value, if no characters are left then it returns -1. In my code it's used as on way to test for end of file. there are other ways available.

Edited 3 Years Ago by tinstaafl

If you don't need to keep track of the index you can use a for-each loop. For example, if you have read the entire text file into a string array, lines(), you can process all of the lines by

For Each line As String In lines
    .
    .
    .
Next
This article has been dead for over six months. Start a new discussion instead.