分裂一个字符串 [英] Splitting up a string

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

问题描述

我是c#的新手,我目前正在尝试制作一个程序来检索来自gamespy服务器的战地2游戏统计数据。我已经得到了它所以我可以检索数据但是我不知道如何将数据切割到

将每个值分配给它自己的变量。所以现在我只是将数据保存到txt文件中,当我查看文本文件时,所有数据都是

那里。


不确定这是否重要,但是当我在Word垫中打开文本文件时

(富文本)显示类似这样的内容。


?? O

H D

asof 1133092198

H D

pid 43974309

nick Opettaja

scor 1459

jond 1118881320

获胜28

亏损47

.....

.....


但是当我在记事本中打开它时(原始文本)一切都在一行上/>
正方形表示换行符。


因此,如果有人对如何将每一行分解为

它有自己的变量有任何建议非常感谢。抱歉,如果我对某些信息有点模糊,我是c#的新手并且还在学习,如果你需要更多信息,请告诉我。


谢谢。

解决方案

" Opettaja" <运算****** @ gmail.com>在留言中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...

< snip>

因此,如果有人对如何将每一行分解成自己的变量有任何建议,我将非常感激。抱歉,如果我对信息有点模糊,我是c#的新手,还在学习,如果你需要更多信息,请告诉我。




这可能会有所帮助:

http://msdn.microsoft.com/library/de...xtFromFile.asp


它一次读取一行文件。记事本的问题可能是* b $ b,直到* nix / Windows上的换行/ carraige返回的差异。它可能是
文件包含\ r或\ n而不是两者。可能需要

a来配置上面的代码用作行尾。


HTH


如果需要,只需确定十六进制编辑器,字段分隔符

和行尾字符是什么。我怀疑你会发现它们分别是

标签和换行符。


一旦你确定了每个字段的界限,你就可以使用

String.Split()方法将字段分解为数组。阅读一行

行,将该行划分为字段,根据需要处理,然后重复,直到

完成。


--Bob


Opettaja写道:

我是c#的新手,我正在尝试制作一个程序来检索来自游戏的战场2游戏统计数据服务器。我已经得到它所以我可以检索数据,但我不知道如何切割数据以将每个值分配给它自己的变量。所以现在我只是将数据保存到txt文件中,当我查看文本文件时,所有数据都在那里。

不确定这是否重要但是当我在Word垫中打开文本文件
(Rich Text)它显示的是这样的东西。

?? O
H D
asof 1133092198
H D
pid 43974309
尼克·奥特塔嘉
scor 1459
jond 1118881320
赢得28
亏损47
....
。 ...

但是当我在记事本中打开它时(原始文本)一切都在一行上,
方块表示换行符。

所以,如果有人有任何建议关于如何将每一行分解成自己的变量,我将非常感激。抱歉,如果我对信息有点模糊,我是c#的新手,还在学习,如果你需要更多信息,请告诉我。

谢谢。


好的我把这些线分成了单独的行,但现在我怎么用
将各行分成2个变量使用Split只用空格分割

。这就是我现在所拥有的


string [] lines = reader.ReadString()。Split(new Char [] {''\ r'',''\ n ''});


for(int i = 0; i< lines.Length; i ++)

{

string [] output = lines [i] .Split(new Char [] {''''}};

listBox1.Items.Add(output [1]);

}


我在Char {'''}中放入什么东西让它被空格分开?


I am new to c# and I am currently trying to make a program to retrieve
Battlefield 2 game stats from the gamespy servers. I have got it so I
can retrieve the data but I do not know how to cut up the data to
assign each value to its own variable. So right now I am just saving
the data to a txt file and when I look in the text file all the data is
there.

Not sure if this matters but when I open the text file in Word pad
(Rich Text) It shows something like this.

?O
H D
asof 1133092198
H D
pid 43974309
nick Opettaja
scor 1459
jond 1118881320
wins 28
loss 47
.....
.....

But when I open it in notepad (Raw Text) Everything is on one line with
squares indicating linebreaks.

So if anyone has any suggestions on how I can break up each line into
its own variable that would be greatly appreciated. And sorry If im a
little vague on information, I am new to c# and still learning, If you
need more information just let me know.

Thanks.

解决方案

"Opettaja" <op******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
<snip>

So if anyone has any suggestions on how I can break up each line into
its own variable that would be greatly appreciated. And sorry If im a
little vague on information, I am new to c# and still learning, If you
need more information just let me know.



This might be helpful:

http://msdn.microsoft.com/library/de...xtFromFile.asp

It reads the file a line at a time. The problem with Notepad is probably
down to the difference in linefeeds/carraige returns on *nix/Windows. It
might be that the file includes \r or \n rather than both. There''s probably
a way to configure what the above code uses as as a line end.

HTH


Simply determine, with a hex editor if need be, what the field delimiter
and end of line characters are. I suspect you''ll find that they are
tabs and line feeds, respectively.

Once you determine what delimits each field then you can use the
String.Split() method to break the fields up into an array. Read one
line, break that line into fields, process as desired, and repeat until
done.

--Bob

Opettaja wrote:

I am new to c# and I am currently trying to make a program to retrieve
Battlefield 2 game stats from the gamespy servers. I have got it so I
can retrieve the data but I do not know how to cut up the data to
assign each value to its own variable. So right now I am just saving
the data to a txt file and when I look in the text file all the data is
there.

Not sure if this matters but when I open the text file in Word pad
(Rich Text) It shows something like this.

?O
H D
asof 1133092198
H D
pid 43974309
nick Opettaja
scor 1459
jond 1118881320
wins 28
loss 47
....
....

But when I open it in notepad (Raw Text) Everything is on one line with
squares indicating linebreaks.

So if anyone has any suggestions on how I can break up each line into
its own variable that would be greatly appreciated. And sorry If im a
little vague on information, I am new to c# and still learning, If you
need more information just let me know.

Thanks.



Okay I got the lines split up into individual lines, but now how do I
split up the individual lines into 2 variables using Split and spliting
by just spaces. Here is what I have right now

string[] lines = reader.ReadString().Split(new Char[] {''\r'',''\n''});

for (int i = 0; i < lines.Length; i++)
{
string[] output = lines[i].Split(new Char[]{'' ''});
listBox1.Items.Add(output[1]);
}

What do I put in the Char{'' ''} to make it split by the spaces?


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

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