English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi guys I am making a program that reads a textfile using a stream reader line by line

each line of the text file is populated with 100 characters
now say I only wanted character(50) from that whole string of letters.

Does anyone know how I can do this

Thanks
Juli

2007-04-14 12:18:44 · 4 answers · asked by julibabyca 1 in Computers & Internet Programming & Design

4 answers

You can use the TextReader.ReadToEnd function and save it as a string. Then, you can use the string.substring(49). That will get the character you want.

'Variable to read text from a file
Dim GetText As System.IO.TextReader
'Get path dialog
Set_SetPath()
'Error handler
Try
'If user pressed "OK"
If OpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then
'Sets the text file to read from
GetText = System.IO.File.OpenText(OpenFile.FileName.ToString)

dim myString as String
myString = GetText.ReadToEnd
txt1.Text = myString.Substring(49)
End If
Catch
End Try

2007-04-14 14:19:59 · answer #1 · answered by Anonymous · 0 0

If you have got the full line into a string variable, use the substring method to get the part of the string you require. In your case 1 - 50.

In the below example, I have put a string of length 10 characters and in the substring, i am extracting only the first 5 characters.

Dim objCompleteString As String
objCompleteString = "1234567890"
MessageBox.Show(objCompleteString.Substring(0, 5))

note - the messagebox line that is getting truncated by yahoo - The truncate bit is
objCompleteString.Substring(0, 5)

In your case you just need to change the line Substring(0, 5) to Substring(0, 50)


Hope that helps

2007-04-14 19:30:39 · answer #2 · answered by Abraham Alex 4 · 0 0

Use:
myStr = microsoft.VisualBasic.Left(str,50)

microsoft.VisualBasic.Left(str,50) (stupid yahoo keeps truncating text)

Basically the Left function takes a string variable and an integer for the number of characters to return on the left.
Ther is a right function as well.

2007-04-15 01:45:03 · answer #3 · answered by MarkG 7 · 0 0

not completely sure of the .net syntax (like everything else in microsoft's world, new version changes everything...)
but in VB 6.0, it would be the sub-string (substr) operator. Try searching the MSDN library for .net to get the sub string syntax.

2007-04-14 19:24:25 · answer #4 · answered by spl 4 · 0 0

fedest.com, questions and answers