Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break a string into Arrays

Tags:

vb.net

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.

like image 887
user1838817 Avatar asked Nov 20 '12 13:11

user1838817


People also ask

Which function breaks a string into an array?

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 .

How do you convert a string to an array in Python?

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.

How do you break apart a string?

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.


2 Answers

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
like image 63
Hannele Avatar answered Nov 15 '22 05:11

Hannele


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
        <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
like image 20
asawyer Avatar answered Nov 15 '22 06:11

asawyer