Take the tour ×

What I have:

So there is this large project I'm working on for school, and I have everything working except for a small but vital piece. The programm I am working on must convert currency, and take the rates from a txt file. The file looks like this:

USD 1,2694
JPY 100,44
BGN 1,955
CZK 25,396
DKK 7,45792

There is a tab break between the name and the value and a line break between the value and the next currency name. Values have a floating point, and don't have a fixed length.

What I need:

I need to break this string into two arrays, currencyNames() and currencyValues(), or into a two-dimentional array currency().

What I can do myself:

I can load it from a file into a string with

fileReader = My.Computer.FileSystem.ReadAllText("rates.txt")

And I was able to break it into an array with a simple loop

Do While i < 32
    dummyArray = Split(fileReader, " ")
    i += 1

but only when there is a space separating the names and values inside the file.

share|improve this question
Try ReadAllLines() instead of ReadAllText() –  Joel Coehoorn Nov 20 '12 at 14:25
add comment

3 Answers

up vote 3 down vote accepted

What you're looking for are the VB Constants, a set of special strings for special characters like tab and new line - there's a list at the link, but yours in particular are vbTab and vbCrLf. You shouldn't need to import anything - they're built in to VB.

To use them, you'd change it to something like:

dummyArray = Split(fileReader, vbCrLf) ' to split on lines

And then:

For Each s as String In dummyArray
  otherArray = Split(s, vbTab) ' to split on tab characters
share|improve this answer
add comment

A combination of ReadLine() and String.Split() should help you solve your problem.

If you were to a read each item line by line, using ReadLine(), you could then split on the space like this:

ReadLine().Split(' ').First(); 


ReadLine().Split(' ').Last(); 

to get the relevant values from your pair.

share|improve this answer
add comment

The basic idea is something like this:

  • Read each line from the file
  • Split the line on the space bar
  • Store the Country as the first portion of the split
  • Store the amount as the second portion, formatted as an integer
  • Project the Country and Amount into seperate arrays

Here's a simple implementation in Vb.Net

Sub Main

    dim input = System.IO.File.ReadAllLines("c:\yourdata.txt")

    dim projection = from line in input
                    let split = line.Split(new string(){" "},StringSplitOptions.RemoveEmptyEntries)
                    select Country = split.First(), Amount = split.Last().Replace(",","").Parse()

    dim countries = projection.Select(function(p) p.Country).ToArray()
    dim amounts = projection.Select(function(p) p.Amount).ToArray()

End Sub

I also used a small extension method to wrap Integer.TryParse

namespace ExtensionMethods
    public module Extensions
        public function Parse(byval value as string) as integer
            dim i = 0
            if integer.TryParse(value,out i) then
                return i
            end if
            return 0
        end function
    end module
end namespace
share|improve this answer
+1 but it's not clear from the source if you split on TAB or SPACE. Shouldn't vbTab be a better solution? .Split(New String() {vbTab}, –  igrimpe Nov 20 '12 at 14:18
@igrimpe I agree, it's not 100% clear. If it should be tab it's a simple enough change. Er to clarify it's not clear in the example data whether it's a space or a tab 100%. I went with the space. It's a good point though. –  asawyer Nov 20 '12 at 14:22
add comment

Your Answer


