Java 什么是解析/解析?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/787735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:30:34  来源:igfitidea点击:

What is Parse/parsing?

javaparsing

提问by

In Java, What exactly is Parsing? Why are they used?

在 Java 中,解析到底是什么?为什么使用它们?

For example: Integer.parseInt(...), and parsing a string?

例如:Integer.parseInt(...),并解析字符串?

回答by tvanfosson

From dictionary.reference.com:

来自dictionary.reference.com

Computers. to analyze (a string of characters) in order to associate groups of characters with the syntactic units of the underlying grammar.

电脑。分析(字符串)以便将字符组与基础语法的句法单元相关联。

The context of the definition is the translation of program text or a language in the general sense into its component parts with respect to a defined grammar -- turning program text into code. In the context of a particular language keyword, though, it generally means to convert the string value of a fundamental data type into an internal representation of that data type. For example, the string "10" becomes the number (integer) 10.

定义的上下文是将程序文本或一般意义上的语言翻译成与定义的语法相关的组成部分——将程序文本转换为代码。但是,在特定语言关键字的上下文中,它通常意味着将基本数据类型的字符串值转换为该数据类型的内部表示。例如,字符串“10”变为数字(整数)10。

回答by Martin Harris

Parsing is to read the value of one object to convert it to another type. For example you may have a string with a value of "10". Internally that string contains the Unicode characters '1' and '0' not the actual number 10. The method Integer.parseInt takes that string value and returns a real number.

解析是读取一个对象的值,将其转换为另一种类型。例如,您可能有一个值为“10”的字符串。该字符串在内部包含 Unicode 字符“1”和“0”,而不是实际数字 10。方法 Integer.parseInt 获取该字符串值并返回一个实数。

String tenString = "10"

//This won't work since you can't add an integer and a string
Integer result = 20 + tenString;

//This will set result to 30
Integer result = 20 + Integer.parseInt(tenString);

回答by OscarRyz

You could say that parsing a string of charactersis analyzing this string to find tokens, or items and then create a structure from the result.

您可以说解析字符串就是分析这个字符串以查找标记或项目,然后根据结果创建一个结构

In your example, calling Integer.parseInton a string is parsing this string to find an integer.

在您的示例中,调用Integer.parseInt字符串是解析此字符串以查找 integer

So, if you have:

所以,如果你有:

String someString = "123";

And then you invoke:

然后你调用:

int i = Integer.parseInt( someString );

You're telling java to analyze the "123"string and find an integer there.

你告诉 java 分析"123"字符串并在那里找到一个整数。

Other example:

其他例子:

One of the actions the java compiler does, when it compiles your source code is to "parse" your .java file and create tokens that match the java grammar.

java 编译器在编译您的源代码时执行的操作之一是“解析”您的 .java 文件并创建与 java 语法匹配的标记。

When you fail to write the source code properly ( for instance forget to add a ;at the end of a statement ), it is the parser who identifies the error.

如果您未能正确编写源代码(例如忘记;在语句末尾添加 a ),则由解析器识别错误。

Here's more information: http://en.wikipedia.org/wiki/Parse

以下是更多信息:http: //en.wikipedia.org/wiki/Parse

回答by dhivya

Parsing is the division of text in to a set of parts or tokens.

解析是将文本划分为一组部分或标记。

回答by hema mahour

Parsing is just process of analyse the string of character and find the tokens from that string and parser is a component of interpreter and compiler.It uses lexical analysis and then syntactic analysis.It parse it and then compile this code after this whole process of compilation.

解析只是分析字符串并从该字符串中找到标记的过程,解析器是解释器和编译器的组成部分。它使用词法分析,然后是句法分析。在整个编译过程之后解析它然后编译此代码.

回答by musicakc

Parsingmeans we are analyzing an object specifically. For example, when we enter some keywords in a search engine, they parse the keywords and give back results by searching for each word. So it is basically taking a string from the file and processing it to extract the information we want.

解析意味着我们正在专门分析一个对象。例如,当我们在搜索引擎中输入一些关键字时,它们会解析关键字并通过搜索每个单词来返回结果。所以它基本上是从文件中取出一个字符串并对其进行处理以提取我们想要的信息。

Example of parsing using indexOfto calculate the position of a string in another string:

indexOf用于计算一个字符串在另一个字符串中的位置的解析示例:

String s="What a Beautiful day!";

int i=s.indexOf("day");//value of i would be 17
int j=s.indexOf("be");//value of j would be -1
int k=s.indexOf("ea");//value of k would be 8

paresIntessentially converts a Stringto a Integer.

paresInt基本上将 a 转换String为 a Integer

String s="9876543";
int a=new Integer(s);//uses constructor
System.out.println("Constructor method: " + a);
a=Integer.parseInt(s);//uses parseInt() method
System.out.println("parseInt() method: " + a);

Output:

输出:

Constructor method: 9876543

parseInt() method: 9876543
Constructor method: 9876543

parseInt() method: 9876543

回答by Sahil Babbar

Parsing can be considered as a synonym of "Breaking down into small pieces" and then analysing what is there or using it in a modified way. In Java, Strings are parsed into Decimal, Octal, Binary, Hexadecimal, etc. It is done if your application is taking input from the user in the form of string but somewhere in your application you want to use that input in the form of an integer or of double type. It is not same as type casting. For type casting the types used should be compatible in order to caste but nothing such in parsing.

解析可以被视为“分解成小块”的同义词,然后分析那里的内容或以修改的方式使用它。在 Java 中,字符串被解析为十进制、八进制、二进制、十六进制等。 如果您的应用程序以字符串的形式从用户那里获取输入,但在您的应用程序中的某个地方您希望以整数或双精度型。它与类型转换不同。对于类型转换,所使用的类型应该是兼容的,以便进行种姓转换,但在解析中没有类似的内容。