Solved

Cutting the first three characters of each line

Posted on 2011-02-21
8
351 Views
Last Modified: 2012-06-21
Hi Experts,

I have a text file that includes different lines and an each line there is some elements divided by a comma (,):
ABC1235,ASD45,A2R45546=4
V5R48654=,TR4548
R58sgd%$,YT841d4,ABC127

What I want is that, I would to load that text file and then cut each element after the third character. So, the final result should be something like this:
ABC,ASD,A2R
V5R,TR4
R58,YT8,ABC

As you can see the above result is cutting each element (the elements divided by comma ,) and then rewrite the output data to the same file.
Thanks in advance
Regards
0
Comment
Question by:Sat80
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 34943651
Several steps to perform:
1. Read the file line by line
2. use the string.substring-method on that line http://msdn.microsoft.com/en-us/library/aka44szs.aspx
3. save everything back into the file


Dim sFileName As String
Dim srFileReader As System.IO.StreamReader
Dim sInputLine As String

dim sOutput as String

sFileName = "test.txt"
srFileReader = System.IO.File.OpenText(sFileName)
sInputLine = srFileReader.ReadLine()
Do Until sInputLine is Nothing
    sOutput += sInputLine.Substring(3, sInputLine.Length() - 3)
    sInputLine = srFileReader.ReadLine()
Loop

Dim objWriter As New System.IO.StreamWriter(sFileName)
objWriter.Write(sOutput)
'Close objectwriter:
objWriter.Close()

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 34943672
Try this:
Public Sub RewriteFile(ByVal filename As String)
	Dim temp As String = System.IO.Path.GetTempFileName()

	Using reader As New System.IO.StreamReader(filename)
		Using writer As New System.IO.StreamWriter(temp)
			While Not reader.EndOfStream
				Dim line As String = reader.ReadLine()
				Dim parts() As String = line.Split(","c)

				For i As Integer = 0 To parts.Length - 1
					parts(i) = parts(i).Substring(0, 3)
				Next

				writer.WriteLine(String.Join(",", parts))
			End While
		End Using
	End Using

	System.IO.File.Move(temp, filename)
End Sub

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34943677
Sorry misread, I understood the string-requirement.
You need to adjust this:
Do Until sInputLine is Nothing
    sOutput += sInputLine.Substring(3, sInputLine.Length() - 3)
    sInputLine = srFileReader.ReadLine()
Loop



into

dim sInput as string()
Do Until sInputLine is Nothing
    sInput = sinputLine.split(',')
    For i = 0 To UBound(aryTextFile)
         sInput(i) = sInput(i).Substring(3, sInput(i)Length() - 3)
     Next i

    sOutput += String.Join(",", sInput)
    sInputLine = srFileReader.ReadLine()
Loop

0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 200 total points
ID: 34943683
Still made a fault
sInput(i) = sInput(i).Substring(3, sInput(i)Length() - 3)
must be
sInput(i) = sInput(i).Substring(0, 3)

Dim sFileName As String
Dim srFileReader As System.IO.StreamReader
Dim sInputLine As String

dim sOutput as String

sFileName = "test.txt"
srFileReader = System.IO.File.OpenText(sFileName)
sInputLine = srFileReader.ReadLine()
dim sInput as string()
Do Until sInputLine is Nothing
    sInput = sinputLine.split(',')
    For i = 0 To UBound(aryTextFile)
         sInput(i) = sInput(i).Substring(0, 3)
     Next i

    sOutput += String.Join(",", sInput)
    sInputLine = srFileReader.ReadLine()
Loop


Dim objWriter As New System.IO.StreamWriter(sFileName)
objWriter.Write(sOutput)
'Close objectwriter:
objWriter.Close()

Open in new window

0
 
LVL 9

Assisted Solution

by:Hawkvalley1
Hawkvalley1 earned 100 total points
ID: 34943731
Dim newList As New List(Of String)
Using sr As New StreamReader(<path>)
 While Not sr.EndOfStream
  Dim str() As String = sr.Readline.Split(","c)
  Dim line As String
  For Each s As String In str
      line &= s.Remove(3) & ","
  Next
  line = line.TrimEnd(","c)
 newLine.Add(line)
 End While
End Using
 
0
 

Author Closing Comment

by:Sat80
ID: 34943794
Thank you all
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34944043
NP. Glad to help  : )
0
 
LVL 9

Expert Comment

by:Hawkvalley1
ID: 34948489
That was fun, how many ways???
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question