Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Put spaces in between characters 1

Status
Not open for further replies.

peebman2000

Programmer
Joined
Nov 28, 2006
Messages
30
Location
US
Hello, I'm a newbie. I'm trying to read a text file and write out the text I only want written out from the text file. The text all smushed together and I don't know how to put spaces in between them. Does anyone know how? My code is below. Thanks.

Imports System.IO
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.Security
Imports System.Web.Configuration
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text
Imports System.Web.Management
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'when the page is loaded
Label1.Visible = False
If Not Page.IsPostBack Then

End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim slnin As String = ""
Dim slnout As String = ""
Dim outputfile As String = ""
'Dim deptid As String
Dim semi As String = ""
Dim national_id As String = ""
Dim last_name As String = ""
Dim mid_initial As String = ""
Dim address1 As String = ""
Dim first_name As String = ""
Dim city As String = ""
Dim state As String = ""
Dim postal As String = ""
Dim phone As String = ""
Dim hire_dt As String = ""
Dim business_title As String = ""
Dim space As String = ""
semi = ";"

If FileUpload1.HasFile Then

'upload file
'Dim filein As String = FileUpload1.HasFile
'Dim objstreamreader As StreamReader
'

'declaring variables

'Dim orig_hire_dte As String = ""
'im filename As String
'Dim objstreamreader As StreamReader
Dim filename As String = ""
If filename = String.Empty Then
filename = FileUpload1.PostedFile.FileName
End If
Dim sr As StreamReader
'Dim streamreader As New StreamReader("c:\mastertestfile.txt")
sr = File.OpenText(filename)

While sr.Peek() <> -1
slnin = sr.ReadLine

'slnin = objstreamreader.ReadLine
'Do While Not strline Is Nothing

'objstreamreader = File.OpenText(filename) 'may undo
'objstreamreader = File.OpenText()'may undo

If Not slnin Is Nothing Then 'store text in slnin
'deptid = slnin.Substring(3000, 1000)
national_id = slnin.Substring(1384, 10) + (" ") + semi
last_name = slnin.Substring(1650, 19)
mid_initial = slnin.Substring(1720, 8)
first_name = slnin.Substring(1685, 15)
address1 = slnin.Substring(1193, 25)
city = slnin.Substring(1305, 13)
state = slnin.Substring(1365, 3)
postal = slnin.Substring(1372, 11)
phone = slnin.Substring(548, 23)
hire_dt = slnin.Substring(188, 21)
business_title = slnin.Substring(33, 18)


'build line to be wrote out
'slnout = deptid
slnout += national_id
slnout += last_name
slnout += mid_initial
slnout += first_name
slnout += address1
slnout += city
slnout += state
slnout += postal
slnout += phone
slnout += hire_dt
slnout += business_title

outputfile += slnout & vbCrLf

End If
End While
Response.Write(slnout)
Response.End()

'If FileUpload1.FileName Then
Dim fileout As String = "\newfile" & ".txt"
Try
File.WriteAllText(Server.MapPath("uploads\" & FileUpload1.FileName) & fileout, outputfile)
Label1.Text = "File uploaded to server" '& FileUpload1.PostedFile.FileName
Catch ex As Exception
''FileUpload1.SaveAs(Server.MapPath("uploads\" & FileUpload1.FileName))
Label1.Text = "Failed because <br/>" & ex.Message
End Try
sr.Close()
'objstreamreader.Close()
Console.ReadLine()

Else
Label1.Visible = True
Label1.Text = "Please select a file to upload"
'End If


'Dim textline As String

'slnin = objreader.ReadLine()

' Label1.Text = "File has been opened"

End If
End Sub
End Class
 
Firstly, use a StringBuilder to create the string to write out to the page otherwise you are creating lots of extra variables in memory that are not needed.

Secondly, just append a space onto each record i.e.
Code:
myStringBuilder.Append(national_id & " ")
myStringBuilder.Append(last_name & " ")
...etc


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ca8msm, thanks for your reply. That helped.
 
If you struggle with multiple spaces, like this:
national_id = slnin.Substring(1384, 10) + (" ") + semi

Try using &nbsp; for space.

eg:
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" instead of " ".

Olav Alexander Mjelde
Admin & Webmaster
 
Try using &nbsp; for space.
Why?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top