473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to separate first and second 8 characters from line and store it in new txt file

hi
i have one txt file that contain data like this

11111111,22222222
22222222,33333333
33333333,44444444


what im trying to do is to make 2 new txt files and to store first 8 characters into first txt and second 8 chars into second txt ... " , " should be excluded

can anyone help me with this

thanks in advice
kristina
Jan 14 '08 #1
6 1145
kadghar
1,295 Expert 1GB
hi
i have one txt file that contain data like this
...
can anyone help me with this

thanks in advice
kristina
Hi Kristina,

sure, use the MID function to take the part of the string you want
lets say you have Srt1 and Str2 as strings, and

Str1= 12345678,12345678

with MID:

Strt2= mid(str1,1,8) '(base string, starting position, length)

you will have Str2= 12345678
other example:

Str3 = ("hello world",3, 5)

will make Str3 = "llo w"

a more general way will be using also INSTR

HTH
Jan 14 '08 #2
it works
thank you so much
Jan 14 '08 #3
Torgg
41
I think this should get you what you want.


Expand|Select|Wrap|Line Numbers
  1.         Dim file_stream As IO.FileStream
  2.         Dim bytes As Byte()
  3.         Dim myArray() As String
  4.         Dim strLine As String
  5.  
  6.         Dim stream_reader = New System.IO.StreamReader("c:\test0.txt") ' Change path to your source file
  7.         While stream_reader.Peek() >= 0
  8.             strLine = stream_reader.readline()
  9.             myArray = Split(strLine, ",")
  10.  
  11.             file_stream = New IO.FileStream("c:\test1.txt", IO.FileMode.Append) 'Change path to the file for the first value
  12.             bytes = New System.Text.UTF8Encoding().GetBytes(myArray(0) & vbCrLf)
  13.             file_stream.Write(bytes, 0, bytes.Length)
  14.             file_stream.Close()
  15.  
  16.             file_stream = New IO.FileStream("c:\test2.txt", IO.FileMode.Append) 'Change path to the file for the second value
  17.             bytes = New System.Text.UTF8Encoding().GetBytes(myArray(1) & vbCrLf)
  18.             file_stream.Write(bytes, 0, bytes.Length)
  19.             file_stream.Close()
  20.         End While
  21.  
  22.         MessageBox.Show("Done")
Torgg
Jan 14 '08 #4
Torgg
41
Or you could parse it in one line of code

Dim arrayValues() As String = Split("33333333,44444444", ",")
MsgBox(arrayValues(0))
MsgBox(arrayValues(1))

Torgg
Jan 14 '08 #5
I done it and there is the code

Expand|Select|Wrap|Line Numbers
  1. Dim str1 As String
  2. Dim str2 As String
  3. Dim str3 As String
  4. Open "c:\1a.pair" For Input As #1     'original file
  5. Open "c:\11.pair" For Output As #2  'new file
  6. Open "c:\12.pair" For Output As #3
  7. Do Until EOF(1) 'read in file until done
  8.   Line Input #1, str1
  9.   str2 = Mid(str1, 1, 32)
  10.   str3 = Mid(str1, 34, 32)
  11.   Print #2, str2
  12.   Print #3, str3
  13. Loop
  14. Close #1
  15. Close #2
  16. Close #3
Thanks once again.
Jan 14 '08 #6
Killer42
8,435 Expert 8TB
str2 = Mid(str1, 1, 32)
str3 = Mid(str1, 34, 32)
I see the specs changed somewhat, along the way. This is where the Split() function would have an advantage, of course - you wouldn't need to recode if the length of the fields changes.
Jan 15 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Matt L. | last post by:
In summary, I don't know how/why the following code works and would like to know. I'm trying to match the first 3 characters of a variable (filename). The code below crudely works, but only if I...
3
by: EJH | last post by:
I have a Database that has three tables. One of the three is just a table that contains three fields and is filled with reference information. One field is 3-Digit(primary key), the next is...
4
by: K | last post by:
I've an XML file in UTF-8. It contains some chinese characters ( both simplified chinese and traditional chinese). In loading the XML file with MSXML parser, I used the below code to retrieve...
3
by: Ze Coder | last post by:
Hello, I would like to know how to split a string variable in x lines(possibly an array) of a maximum length of 30 chrs, but without truncating words. I Think there's a trick to do with...
1
by: Slavo Smutny | last post by:
Hi, which is better approach to store my JavaScript code, to store it in separate .js file or to embed the code within HTML attributes (e.g. <p onclick="javascript:submit();">click this</p>)? I...
0
by: hagar | last post by:
Hi all, I have a problem which I can not understand why this is happening! Debugging this I actually see that it grabs first record then when stepping through code to the line rsImportTo.AddNew...
6
by: Registered User | last post by:
Hi experts, I'm trying to write a program to solve the following exercise: Accept three strings from the user. Find the first occurrence of the first string in the third string. If it is present...
3
by: cs | last post by:
Hi, I'm new to C and would appreciate any feedback on the following program, asplit, which splits a file into 2 new files, putting a certain number of lines in the first file, and all the rest...
45
by: Dennis | last post by:
Hi, I have a text file that contents a list of email addresses like this: "foo@yahoo.com" "tom@hotmail.com" "jerry@gmail.com" "tommy@apple.com" I like to
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.