字符串数组成整数数组? [英] string array into an integer array?

查看:114
本文介绍了字符串数组成整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助将字符串数组转换为整数数组。我正在读取从文本文件(test.txt)形成数组的值。

i need help converting a string array into an integer array. i'm reading in my values that are forming the array from a text file (test.txt).

推荐答案

有什么问题?对于字符串数组的每个itme,使用 Int32.Parse [ ^ ](或 TryParse [ ^ ])将字符串转换为数字并将获得的数字添加到整数数组的方法。

您也可以跳过'fill-the-array-of-strings'步骤并直接生成整数数组。
What is the problem? For each itme of the array of strings use Int32.Parse[^] (or TryParse[^]) method to convert the string into a number and add the obtained number to the integer array.
You may also skip the 'fill-the-array-of-strings' step and produce directly the array of integers.


基本上,你应该创建一个目标类型的数组,并在循环中逐个元素地转换它。但是,在.NET FCL中有一个方便的通用泛型方法,可以在一行代码中完成: http ://msdn.microsoft.com/en-us/library/exc45z53.aspx [ ^ ]。



-SA
Essentially, you should create an array of the target type and convert it element by element, in a loop. However, there is a convenient universal generic method in .NET FCL, to do it in one line of code: http://msdn.microsoft.com/en-us/library/exc45z53.aspx[^].

—SA


试试这个,



Try This,

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Readtxt As String = "1,2,3,4,5"
        Dim arrTxt() As String = Split(Readtxt, ",")
        Dim arrLength As Integer = arrTxt.Length
        Dim arrNumber(arrLength - 1) As Integer
        For i As Integer = 0 To arrLength - 1
            arrNumber(i) = Integer.Parse(arrTxt(i))
            
        Next
    End Sub


这篇关于字符串数组成整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆