简体   繁体   English

C#和VB.NET中的拆分方法

[英]The Split method in C# and VB.NET

Thanks for someone who might be able to help me further, as I can't find an answer in the www right now.感谢可能能够进一步帮助我的人,因为我现在无法在 www 中找到答案。 I actually program VB.net, but since this language is now dissolved and not further developed, I want to switch to C#.我实际上编程了 VB.net,但是由于这种语言现在已经解散并且没有进一步开发,所以我想切换到 C#。 I am getting on well, because a lot of things are similar.我相处得很好,因为很多事情都是相似的。 There is only one method where I am stuck.我被卡住的方法只有一种。

In vb.net I always liked to use the method:在 vb.net 中我一直喜欢用这个方法:

mystring.split(seperator)(part) 

to get a certain string from a CSV line and work with it.从 CSV 行获取某个字符串并使用它。 How does this work in C#?这在 C# 中如何工作? I know that I can load everything into an array, but I don't want to do that in this case.我知道我可以将所有内容加载到数组中,但在这种情况下我不想这样做。 A simple split and the number of the element is enough.一个简单的拆分和元素的数量就足够了。 Can someone help me?有人能帮我吗?

Seems like the same split method is what you need?似乎您需要相同的拆分方法? It returns an array which you could store or just access directly.它返回一个您可以存储或直接访问的数组。 The difference being in C# you use [index] instead.不同之处在于 C# 您使用[index]代替。 So something like this:所以是这样的:

string myFile = File.ReadAllText("myfile.txt");
string firstLine = myFile.Split("\r\n")[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM