how can we find the spacific text in the text file?

how can we find the spacific text in the text file

for eg

ABC12345

ABC48587

ABC75891

acctually i want to get the 5 numbers after abc

thanks

[183 byte] By [AtherAbbas] at [2007-12-24]
# 1

you could read through each line and check the text on the fly:

Dim theStreamReader as new System.IO.StreamReader("filename.txt")

while theStreamReader.Peak() > -1

dim theCurrentLine as string = theStreamReader.ReadLine()

'current line in file is stored in the variable above, now do whatever checking it is you would like to

end while

of course performance will decrease if you have a big file, since you are iterating through each line.

to get specific text, you could use Regex (regular expression) to do a pattern matching technique which is great.

You could also use another nasty way, using string parsing etc... so if I had the text:

ABC12345

and wanted to get everything after ABC, and I know that I have ABC in the text:

Dim theString as string = "ABC12345"

Dim theTextIWant as string = theString.SubString(theString.IndexOf("ABC"))

would get me everything after "ABC"

does this help?

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

thanks a lot

acctually i have problem to get the 5 numbers after ABC. for eg

fkj;dsfjajdfjd;fljdkfjfjABC12345ffjkfdjfkdjkjdkfjfjdkjfkdfj

dlfkjdjffpeureoiuiureiuierABC67890fkdlfkdkfdfkf

this is the situation....

how can i get only 5 number after ABC and all the number are store in array....

thanks

AtherAbbas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Dim theString as string = "ABC12345"

Dim theTextIWant as string = theString.SubString(theString.IndexOf("ABC"),5)

you put the number o chars you want to coppy in TextIWant

gtimofte at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

as suggested, read the line then do a substring and index of:

'create an array:

Dim theArrayList as new ArrayList()

'While loop from the above code sample given

Dim currentLine as String = theCurrentLine.ReadLine()

Dim theString as String = currentLine.SubString(currentLine.IndexOf("ABC"), 5)

theArrayList.Add(theString)

'end while

this will:

  • read the line(s) in a loop, modified code from the above

  • get the string you are after, with a length of 5 - so ABC12345 should be returned as 12345

  • stores it into the array

    you may wish to do some error trapping also as the code I posted was just rough and straight to the point :-)

  • ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...