Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
Go Back  Xtreme Visual Basic Talk > > > Add a carriage return after every 100 characters in a text file


Reply
 
Thread Tools Display Modes
  #1  
Old 02-08-2006, 02:37 AM
Prash Prash is offline
Newcomer
 
Join Date: Feb 2006
Location: London, UK
Posts: 3
Default Add a carriage return after every 100 characters in a text file


Hi,

I have a very large single string in a text file. I need to add a carriage return (new line) after exactly every 100 characters.

Is this possible using VB.net? If so please could someone provide some sample code.

Thanks

Prash
Reply With Quote
  #2  
Old 02-08-2006, 03:24 AM
shaul_ahuvaAdd a carriage return after every 100 characters in a text file shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

It's a relatively simple operation, but I would suggest using the StringBuilder class to avoid lots of junk on the heap. The basic flow would be something like:
  1. Create a StringBuilder with a default value of the string you want to insert carriage returns into.
  2. Create a variable to keep track of the current index to insert a carriage return at and set it to 100.
  3. Loop while the index to insert a carriage return at is less than the StringBuilder's length.
  4. For every iteration of the loop, insert a carriage return using StringBuilder.Insert and increase the index variable by 100.
If you don't know how to read/write the file, I would suggest looking at the StreamReader and StreamWriter class refererences on MSDN.
Reply With Quote
  #3  
Old 02-08-2006, 04:52 AM
Prash Prash is offline
Newcomer
 
Join Date: Feb 2006
Location: London, UK
Posts: 3
Default Thanks

Hi,

Thanks for that I got it working.

Below is the code incase someone else needs it.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oRead As System.IO.StreamReader
Dim EntireFile As String
Dim oFile As System.IO.File
oRead = oFile.OpenText("C:/rentbacs")
EntireFile = oRead.ReadToEnd ' reads contents of file into a variable

Dim sb As New System.Text.StringBuilder(EntireFile) ' creates new stringbuilder instance using Entirefile var
System.Console.WriteLine(sb.Length) ' prints length of your string


'Now loop through the stringbuilder string and use insert command
Dim count As Integer = 0
Dim tempcount As Integer = 0

While count <= sb.Length

If tempcount = 100 Then
sb.Insert(count, vbCrLf)
'reset tempcount, so we start searching for next 100 characters
tempcount = 0
End If

tempcount = tempcount + 1
count = count + 1
End While

'newly edited string
Dim mainString As String = sb.ToString

'Now we write back the new string to the file
Dim fs2 As New FileStream("C:/rentbacs", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim s2 As New StreamWriter(fs2)
'overwriting string to file
s2.Write(mainString)
'closing the file
s2.Close()

End Sub


Thanks Again

Prashant
Reply With Quote
  #4  
Old 02-08-2006, 06:18 AM
shaul_ahuvaAdd a carriage return after every 100 characters in a text file shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

I probably would have done something like:

Code:
Dim sr As New StreamReader(...) Dim sb As New StringBuilder(sr.ReadToEnd()) Dim currentIndex As Int32 = 100 While currentIndex < sb.Length sb.Insert(currentIndex, vbCrLf) currentIndex += 102 '100 characters + vbCrLf.Length (2) End While

This moves the index relative to how many times you've inserted a cr/lf. Since you only really need to insert for 100 characters until you go past the end of the string, you don't really need to count to 100. Otherwise, you go through the loop once for each character in the string...
Reply With Quote
  #5  
Old 02-08-2006, 06:50 AM
Prash Prash is offline
Newcomer
 
Join Date: Feb 2006
Location: London, UK
Posts: 3
Default

good point, thanks.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
 
Add a carriage return after every 100 characters in a text file
Add a carriage return after every 100 characters in a text file
 
-->