开发者

split string, VB.net?

开发者 https://www.devze.com 2023-03-01 13:22 出处:网络
I have a file txt file that holds 3 values each seperated by a space how can i a开发者_如何学Gossign each value to its own variable and use that for other things?

I have a file txt file that holds 3 values each seperated by a space how can i a开发者_如何学Gossign each value to its own variable and use that for other things?

as example the numbers might be displayed in the text file as:

-1100.02 -1958.19 0.0


Translating Marco’s C# code to VB:

Dim s As String = File.ReadAllText(filename)
Dim nums As String() = s.Split(" "c)

To get numbers, you need to parse the strings separately. You can use Linq to do this:

Dim numbers As Double() = From num In nums Select Double.Parse(num)


In C#:

string s = File.ReadAllText(filename);
string[] nums = s.Split(' ');

So you can access nums[index] where index should be between 0 and 2.
Note that you MUST check if everything went ok...

If you need you can also try:

foreach (string num in nums)
{
    double d = double.Parse(num);
    // Here you can do what you want with d
}


Try this:

Dim line as String = "-1100.02 -1958.19 0.0"
Dim values() as Double = Array.ConvertAll(line.Split(New Char() { " "c }, StringSplitOptions.RemoveEmotyEntries), AddressOf Convert.ToDouble)

This will result in values being filled with numbers from the input string (assuming a set of valid numbers on each line).


dim strSplitted() as string = Line.split(" "c) 
' strSplitted(0), strSplitted(1) and strSplitted(2) will hold the values.

Line is the line in the file ofcourse :-)

update: code updated according to comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消