分隔两个数字. [英] Seperating two numbers.
问题描述
我正在从VB.NET转到C#的过程中,我想知道是否有人可以帮助我解决我遇到的问题.
基本上,我是从RSS提要中下载一些XML坐标,但是我不知道如何分隔两个不同的数字,并将它们放入称为lat.Text和lon.Text的分隔文本框中.如果它们的长度相同,我会知道该怎么做,但事实并非如此,这会让我头疼.
这是数据的样本.
51.103 1.800
50.400 0.000
50.3 0.2
50.3 -0.3
Hi,
I am in the process of moving over from VB.NET to C# and I was wondering if anyone could help me with a problem I have been having.
Basically I am downloading some XML coordinates from an RSS feed but I don''t know how I would seperate the 2 different numbers and put them into seperated text boxes called, lat.Text and lon.Text. I would know how to do it if they were the same length but as they are not it''s causing me a headache.
Here is a sampele of the data.
51.103 1.800
50.400 0.000
50.3 0.2
50.3 -0.3
推荐答案
使用String.Split()
.
例如,如果您在"51.103 1.800"上调用此代码:
UseString.Split()
.
As an example, if you called this on "51.103 1.800" :
string data = "51.103 1.800";
string[] lines = data.Split();
现在lines
将有2个项目,lines[0]
将为"51.103",而lines[1]
将为"1.800".
*更新*
----------
[感谢约翰]
请注意,我如何不将任何参数传递给Split
.发生的事情是它将根据任何空白字符(制表符,空格,多个空格,换行符等)进行拆分.
如果您特别想在一个空格上分割,请按照以下步骤操作:
Now lines
would have 2 items, lines[0]
would be "51.103" and lines[1]
would be "1.800".
*Update*
----------
[Thanks to John]
Note how I don''t pass any parameter to Split
. What happens is that it will split based on any white space character (tab, space, multiple spaces, newline etc.)
If you specifically want to split on a single space, do it as follows:
data.Split(' ');
*另一个更新*
--------------------
[感谢GenJerDan]
如果要确保返回的数组不包含任何空字符串,则可以使用带有StringSplitOptions
参数的重载.
示例:
*Another Update *
--------------------
[Thanks to GenJerDan]
If you want to be sure that the returned array won''t have any empty strings, you can use an overload that takes a StringSplitOptions
argument.
Example:
string[] lines = data.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
您不能在重载中利用params功能,因此您需要对第一个参数使用实际的数组创建语法.
You cannot take advantage of the params feature in this overload so you need to use actual array creation syntax for the first parameter.
这篇关于分隔两个数字.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!