【问题标题】:VB.Net concatenation syntax [duplicate]VB.Net 连接语法
【发布时间】:2016-06-13 04:24:55
【问题描述】:

在我们周围,我看到了很多用这些操作码编写的代码; +=&= 我知道它们与串联有关。那么有人可以向我解释+=&=+& 相比有什么区别。

提前谢谢你。

【问题讨论】:

  • += 是加法。比var = var + 1 更简单; & 用于字符串,&= 用于将某些值连接到更长字符串的小循环
  • 除了 Plutonix 的注释之外,+可以用于字符串连接,但不推荐,因为它在某些情况下可能会导致异常。 & 应始终用于字符串连接。

标签: vb.net syntax concatenation


【解决方案1】:


+= 运算符是X = X + Y 的缩写形式 + 运算符通常用于求和而不是字符串组合(See Here)。示例:

'Setting Values
Dim Var As Integer
Var = 101
'Adding 62 to this number (SHORT FORM)
Var += 62 'This will set Var to 163
'Reset value
Var = 101
'This is standard long form
Var = Var + 62 'This will again set Var to 163

&= 运算符是 String1 = String1 & String2 的缩写形式 & 运算符是字符串组合。示例:

'Setting Values
Dim String1 As String
String1 = "coding is "
'combine "Great" to this string (SHORT FORM)
String1 += "Great!" 'This will set String1 to "coding is Great!"
'Reset Value
String1 = "coding is "
'This is standard long form
String1 = String1 & "Great!" 'This will again set String1 to "coding is Great!"

【讨论】:

    【解决方案2】:

    其实没有什么区别,只是写这个比较长

    MyString = MyString & "some text"
    

    比这个

    MyString &= "some text"
    

    由于程序员是非常懒惰的人......

    数字的加号也是如此。 (我知道它也可以用于字符串,但不推荐使用...)

    【讨论】:

    • 这并不能真正回答 OP 问题:So can someone explain to me what is the difference between += and &=
    • ...与 + 和 & 相比。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    相关资源
    最近更新 更多