0

I am inputing numbers separated by commas. I need to store these numbers in an array of double elements, ignoring any other character the user has input. But the problem is that TextBox indices and array indices are different, and also 2.4 is stored as each separate elements.

For example, I have a string like this

"1,2.4,5.4,6,2"

How can I convert this to an array with elements

(1),(2.4),(5.4),(6),(2)
4
  • Do you need error control? Can de string be 1,2,potato,5.6,8? Or it will be for sure correct numbers spearated by comas? Also, dou you need them on an array or a List is viable? Commented Jul 4, 2016 at 10:49
  • Yeah,It can,Thats why i need to ignore character other than numbers,But this ignore the floating point also, Commented Jul 4, 2016 at 10:51
  • I have not touch java in a while, so somoene better than me translate this into code. But just: create an array of string with the split, create a List of doubles, in a for loop iterate the string array, in each iteration try to parse to double and add it to the list. Commented Jul 4, 2016 at 10:55
  • 2
    @Aimnox : You wrote that comment the same time as I wrote an answer about exactly that :). -- Also this is VB.NET, not Java. (: Commented Jul 4, 2016 at 11:06

1 Answer 1

2

Utilizing the String.Split() function and combining that with a For-loop should give you the result that you want.

This will also check if it actually is a number or not, to avoid errors:

Public Function StringToDoubleArray(ByVal Input As String, ByVal Separators As String()) As Double()
    Dim StringArray() As String = Input.Split(Separators, StringSplitOptions.RemoveEmptyEntries) 'Split the string into substrings.
    Dim DoubleList As New List(Of Double) 'Declare a list of double values.

    For x = 0 To StringArray.Length - 1
        Dim TempVal As Double 'Declare a temporary variable which the resulting double will be put in (if the parsing succeeds).
        If Double.TryParse(StringArray(x), TempVal) = True Then 'Attempt to parse the string into a double.
            DoubleList.Add(TempVal) 'Add the parsed double to the list.
        End If
    Next

    Return DoubleList.ToArray() 'Convert the list into an array.
End Function

The Separators As String() parameter is an array of strings that the function should split your string by. Every time you call it you can initialize a new array with the separators you want (a single separator is fine).

For example:

StringToDoubleArray("1,2;3", New String() {",", ";"})

The above will split by commas (,) and semicolons (;).

Example usage for your purpose:

Dim Values As Double() = StringToDoubleArray(TextBox1.Text, New String() {","}) 'Uses a comma as the separator.

Update:

Online test: http://ideone.com/bQASvO

Sign up to request clarification or add additional context in comments.

6 Comments

Thank You :) . Perfect answer
@Half-Bloodprince : Glad I could help, and thank you!
@VisualVincent - one minor critique. You are possibly parsing the strings twice. Instead of using Nothing in the first one, send the result to a variable and if the parse is good use that variable in the list add.
@dbasnett : Thanks for the heads-up! I was already aware of it when I wrote the answer, but I just didn't bother doing so as I was in no position to test it. Though it's not a big bottleneck unless you'd parse a really big array. I will rewrite the code now.
@Half-Bloodprince : I've updated the code to avoid parsing the string multiple times. Please see (I also updated the online test).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.