分割字符串,VB.net?

发布于 2024-11-03 08:01:03 字数 122 浏览 1 评论 0原文

我有一个 txt 文件,其中包含 3 个值,每个值均以空格分隔,我如何将每个值分配给它自己的变量并将其用于其他用途?

例如,数字可能在文本文件中显示为:

-1100.02 -1958.19 0.0

I have a file txt file that holds 3 values each seperated by a space how can i assign 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妞丶爷亲个 2024-11-10 08:01:03

将 Marco 的 C# 代码翻译为 VB:

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

要获取数字,您需要单独解析字符串。您可以使用 Linq 来执行此操作:

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

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)
此生挚爱伱 2024-11-10 08:01:03

在 C# 中:

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

因此您可以访问 nums[index],其中索引应介于 0 和 2 之间。
请注意,您必须检查一切是否正常...

如果您需要,您也可以尝试:

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

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
}
空城缀染半城烟沙 2024-11-10 08:01:03

试试这个:

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)

这将导致 values 被输入字符串中的数字填充(假设每行都有一组有效数字)。

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).

香草可樂 2024-11-10 08:01:03
dim strSplitted() as string = Line.split(" "c) 
' strSplitted(0), strSplitted(1) and strSplitted(2) will hold the values.

当然,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文