string CStr() Str() .ToString()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8924803/
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-09-09 01:21:38  来源:igfitidea点击:

CStr() Str() .ToString()

vb.netstring

提问by aer

I just want to know, what exactly the difference between the functions CStr()and Str()and also the .ToString()? With the code provided below, what's the difference between the three?

我只想知道,函数CStr()Str()以及.ToString()之间究竟有什么区别?使用下面提供的代码,三者之间有什么区别?

Label1.Text = CStr(Int(Rnd() * 10))

and

Label1.Text = Str(Int(Rnd() * 10))

and

Label1.Text = Int(Rnd() * 10).ToString

When I used this condition

当我使用这个条件时

If Label1.Text = "7" Then
     'Some code here
End If

...the Str()function didn't work here. What difference did it make? thanks in advance :))

... Str()函数在这里不起作用。它有什么不同?提前致谢 :))

回答by John Woo

ToStringwill call the .ToString()function on a particular instance. In practice, this means that it will throw an exception if the object in question is Nothing. However, you can implement .ToString()in your own classes to get a useful string representation of your object, whereas CType/CStronly work with built-in classes and interfaces.

ToString.ToString()在特定实例上调用该函数。实际上,这意味着如果有问题的对象是 ,它将抛出异常Nothing。但是,您可以.ToString()在自己的类中实现以获得对象的有用字符串表示,而 CType/CStr仅适用于内置类和接口。

CStrand CType(expression, String)are exactly equivalent (I'm not sure where the other poster got the idea that CStris faster). But they aren't really functions, they're compiler directives that will emit very different code depending on the declaration of expression. In most cases, these directives call a bunch of internal VB code that tries to get a reasonable string out of expression.

CStrCType( expression, String)是完全等效的(我不确定另一张海报从哪里得到CStr更快的想法)。但它们并不是真正的函数,它们是编译器指令,会根据expression的声明发出非常不同的代码。在大多数情况下,这些指令会调用一堆内部 VB 代码,试图从expression 中获取合理的字符串。

DirectCast(expression, String)assumes that the expression in question really is a Stringand just casts it. It's the fastest of all these options, but will throw an exception if expressionis anything other than a String.

DirectCast( expression, String)假设所讨论的表达式确实是 aString并且只是将其强制转换。它是所有这些选项中最快的,但如果表达式不是 a ,则会抛出异常String

回答by Unhandled Exception

As an Addition to the VBA/VB6 Environment where we have no ToString():

作为对 VBA/VB6 环境的补充,我们没有ToString()

  • Str()is not awareof international representation. The decimal separator always is a dot (.).

    As already mentioned above it prefixes the resulting string with a blank in case of positive values.

  • There also exists Str$(). The difference to Str()is the return type:

    Str()returns a variant of type string, Str$()returns a string.

    And Str$()is slightly faster then Str().

  • CStr()in contrast is awareof international representation. The decimal separator depends on the Windows international settings.

    No additional prefixing for positive values will be done.

  • Str()不知道国际代表性。小数点分隔符始终是一个点 ( .)。

    正如上面已经提到的,如果是正值,它会在结果字符串前面加上一个空格。

  • 也存在Str$()。区别Str()在于返回类型:

    Str()返回字符串类型的变体,Str$()返回字符串。

    然后Str$()稍微快一点Str()

  • CStr()相比之下,知道国际代表性。小数点分隔符取决于 Windows 国际设置。

    不会为正值添加额外的前缀。

So if you need to convert a value type to a string and have to ensure a dot as a decimal separator and no prefixing blank, then use this syntax:

因此,如果您需要将值类型转换为字符串,并且必须确保点作为小数点分隔符并且没有前缀 blank,请使用以下语法:

Dim d As Double
d = 123.456

Dim s As String
s = Trim(Str$(d))

回答by Mahdi Jazini

I don't know about ToString() and i don't know about VB.NET

我不知道 ToString() 也不知道 VB.NET

But in VB6 (Visual Basic 6):

但在 VB6 (Visual Basic 6) 中:

Both of Cstr() and Str() converts values to string. but Cstr() is better because:

Cstr() 和 Str() 都将值转换为字符串。但 Cstr() 更好,因为:

Str():After converting to string it adds 1 space before positive numbers. for example: Str(22) > " 22"

Str():转换为字符串后,在正数前加1个空格。例如:Str(22) > " 22"

Cstr():After converting to string it never adds the above extra space - For best result use it with Trim() - Trim(Cstr(Variable))

Cstr():转换为字符串后,它永远不会添加上述额外空间 - 为了获得最佳结果,将它与 Trim() - Trim(Cstr(Variable)) 一起使用

回答by robotik

Although not a problem in the code in the question, it is important to mention that Str()only converts numerical expressions to string, gives an error in other cases, so don't use it for converting values of a cell.

虽然问题中的代码不是问题,但重要的是要提到Str()仅将数字表达式转换为字符串,在其他情况下会出错,因此不要将其用于转换单元格的值。