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
Loop
but only when there is a space separating the names and values inside the file.
Using split() If the string and separator are both empty strings, an empty array is returned. The following example defines a function that splits a string into an array of strings using separator .
The most straightforward way is to type cast the string into a list. Tyepcasting means to directly convert from one data type to another – in this case from the string data type to the list data type. You do this by using the built-in list() function and passing the given string as the argument to the function.
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
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
The basic idea is something like this:
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
<Extension()>_
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With