简体   繁体   English

是否有单声道(.Net)与Java中的Scanner类相同

[英]Is there a mono (.Net) equivalent to the Scanner-class in Java

In Java there is a nice class called Scanner for reading strings and streams of a certain format to data-structures like integers, floats,... Is there any equivalent in Mono? 在Java中有一个很好的类叫做Scanner,用于读取特定格式的字符串和流到数据结构,如整数,浮点数......在Mono中是否有任何等价物? In .Net there is a binary-reader, but it reads the data as binaries. 在.Net中有一个二进制阅读器,但它将数据作为二进制文件读取。 I want a class that read the data by parsing it from a string. 我想要一个通过从字符串中解析数据来读取数据的类。

EDIT: reaction on the current answers, I know most of the data-types have a parse-method, but the problem with these are, they don't read the string sequentially. 编辑:对当前答案的反应,我知道大多数数据类型都有一个解析方法,但问题是,它们不会按顺序读取字符串。 In Java you simply initialize the object, and by activating methods, you sequentially read the data-types. 在Java中,您只需初始化对象,通过激活方法,您可以按顺序读取数据类型。

Well, since you are reading a file in for test cases, I'm going to assume you know the exact format of the data coming in and how it needs to be parsed. 好吧,既然您正在为测试用例读取文件,我将假设您知道进入数据的确切格式以及如何解析它。 Looking at Java's Parser class in the documentation does make it look very complicated or much more than a convenience class. 在文档中查看Java的Parser类确实使它看起来非常复杂或者不仅仅是一个便利类。 It appears to just tokenize the stream string on spaces and provide functions for taking the token and parsing it. 它似乎只是在空格上标记化流字符串,并提供获取令牌和解析它的函数。 And as far as I know, .Net doesn't have an equivalent class. 据我所知,.Net没有相应的类。 So if you want one for reuse, then you'll need to write one yourself. 因此,如果您想要重用一个,那么您需要自己编写一个。

But the basic algorithm you need is to read the file contents, split the tokens, and parse them. 但您需要的基本算法是读取文件内容,拆分标记并解析它们。 If your file is reasonable sized, you could use File.ReadAllLines() which reads the contents of the file line-by-line into a String[] . 如果文件大小合理,可以使用File.ReadAllLines()将文件内容逐行读入String[] Or you could use a StreamReader class to read the lines one at a time using its ReadLine() . 或者您可以使用StreamReader类使用其ReadLine()一次读取一行。 Then once you have them in a line, you would tokenize them with String 's Split() , and then parse them. 然后,一旦你将它们放在一行中,你就可以使用StringSplit()它们进行标记,然后解析它们。

Not knowing exactly how you need to use the values in the file, a very basic pattern could be: 不知道你需要如何使用文件中的值,一个非常基本的模式可能是:

// Read in the whole file
String[] strLines = File.ReadAllLines(strFilePath);

// Loop through each of the lines to process their contents
foreach (String strLine in strLines) {
    // Tokenize on the spaces
    String[] strTokens = strLine.Split(' ');

    // Parse the values
    int iIntValue = Int.Parse(strTokens[0]);
    double dDoubleValue = Double.Parse(strTokens[1]);

    // Do something with the values
}

Creating your own Parser class would allow you to maintain the state and auto advance tokens as you use them. 创建自己的Parser类可以让您在使用时保持状态和自动前进令牌。 If you go that route, the StreamReader may be the best option. 如果你走那条路, StreamReader可能是最好的选择。

Most basic data types will have a .Parse and .TryParse() methods that will do that for you. 大多数基本数据类型都有.Parse.TryParse()方法,可以为您完成。

For example the Int32.TryParse method. 例如Int32.TryParse方法。

In .NET, many of the type have static .Parse() and .TryParse() methods that takes a String parameter. 在.NET中,许多类型都具有静态.Parse().TryParse()方法,这些方法接受String参数。

You could do something like: 你可以这样做:

Stream s = new Stream();
// Wire up the stream.

StreamReader reader = new StreamReader(s);
String value = reader.ReadToEnd();

int valueAsInt = Int32.TryParse(value);

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

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