ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 7252|回复: 12

[推荐]“&”和 “+” 的区别

[复制链接]

TA的精华主题

TA的得分主题

发表于 2006-1-13 15:17 | 显示全部楼层 |阅读模式
The Difference Between & and + There are times when you may need to concatenate something to a string, but only if the string actually has a value. For example, you may want to include the Middle Initial in a person’s full name. If you write code like this strFullName = FirstName & " " & MiddleInitial & " " & LastName you will have a small problem. People with no middle name(it is null in the table) will have two spaces between their first and last names, like this: Tom Smith Fortunately, there is another concatenation operator: ”+”. The technical explanation of this operator is “concatenation with null propagation.” That’s a great phrase to impress your friends with at parties, butan easier explanation is that it concatenates two strings like the ”&” operator, but only if both strings have a value. If either one is Null, the result of the whole concatenation operation is Null. +连接两个非Null字符串时的作用与&相同,但只要其中的一个字串为Null值,则连接结果就会是Null Using our FullName example, the goal is to have only onespace separating First and Last names if there is no Middle Initial. Using +, we can tack on the extra space only if the Middle Name is not null: MiddleName + " " The whole thing looks like this: strFullName = FirstName & " " & (MiddleInitial + " ") & LastName 在MiddleInitial为Null值时,(MiddleInitial + " ")中的“+”避免了用“&”时在FirstName与LastName中出现两个空格的不合理结果。 Notice that you can use parentheses () to ensure that the operations happen in the correct order. In this case, the inner phrase (MiddleInitial + " ") will evaluate to the Middle Initial plus a space, or to null (if there is no middle initial). Then, the rest of the statement will be performed.
[此贴子已经被作者于2006-1-13 15:18:44编辑过]

TA的精华主题

TA的得分主题

发表于 2006-1-13 15:20 | 显示全部楼层
谢谢分享

TA的精华主题

TA的得分主题

发表于 2006-1-16 08:44 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2006-2-6 11:08 | 显示全部楼层
能翻译出来吗?感觉很有必要了解他们的异同与功能,但是E文看的很难受啊!
以下是引用[I]FENGJUN[/I]在2006-1-13 15:17:44的发言:[BR]The Difference Between & and + There are times when you may need to concatenate something to a string, but only if the string actually has a value. For example, you may want to include the Middle Initial in a person’s full name. If you write code like this strFullName = FirstName & " " & MiddleInitial & " " & LastName you will have a small problem. People with no middle name(it is null in the table) will have two spaces between their first and last names, like this: Tom Smith Fortunately, there is another concatenation operator: ”+”. The technical explanation of this operator is “concatenation with null propagation.” That’s a great phrase to impress your friends with at parties, butan easier explanation is that it concatenates two strings like the ”&” operator, but only if both strings have a value. If either one is Null, the result of the whole concatenation operation is Null. +连接两个非Null字符串时的作用与&相同,但只要其中的一个字串为Null值,则连接结果就会是Null Using our FullName example, the goal is to have only onespace separating First and Last names if there is no Middle Initial. Using +, we can tack on the extra space only if the Middle Name is not null: MiddleName + " " The whole thing looks like this: strFullName = FirstName & " " & (MiddleInitial + " ") & LastName 在MiddleInitial为Null值时,(MiddleInitial + " ")中的“+”避免了用“&”时在FirstName与LastName中出现两个空格的不合理结果。 Notice that you can use parentheses () to ensure that the operations happen in the correct order. In this case, the inner phrase (MiddleInitial + " ") will evaluate to the Middle Initial plus a space, or to null (if there is no middle initial). Then, the rest of the statement will be performed.

TA的精华主题

TA的得分主题

发表于 2006-2-20 11:00 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2006-9-24 00:18 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2006-9-25 09:57 | 显示全部楼层
QUOTE:
以下是引用chenhuafu在2006-2-6 11:08:45的发言:
能翻译出来吗?感觉很有必要了解他们的异同与功能,但是E文看的很难受啊!
QUOTE:
以下是引用[I]FENGJUN[/I]在2006-1-13 15:17:44的发言:[BR]The Difference Between & and +
There are times when you may need to concatenate something to a string, but only if the string actually
has a value. For example, you may want to include the Middle Initial in a person’s full name. If you
write code like this
strFullName = FirstName & " " & MiddleInitial & " " & LastName
you will have a small problem. People with no middle name(it is null in the table) will have two spaces
between their first and last names, like this:
Tom  Smith
Fortunately, there is another concatenation operator: ”+”. The technical explanation of this operator is
“concatenation with null propagation.” That’s a great phrase to impress your friends with at parties, butan easier explanation is that it concatenates two strings like the ”&” operator, but only if both strings have a value. If either one is Null, the result of the whole concatenation operation is Null.
+连接两个非Null字符串时的作用与&相同,但只要其中的一个字串为Null值,则连接结果就会是Null
Using our FullName example, the goal is to have only onespace separating First and Last names if there
is no Middle Initial. Using +, we can tack on the extra space only if the Middle Name is not null:
MiddleName + " "
The whole thing looks like this:
strFullName = FirstName & " " & (MiddleInitial + " ") & LastName
在MiddleInitial为Null值时,(MiddleInitial + " ")中的“+”避免了用“&”时在FirstName与LastName中出现两个空格的不合理结果。
Notice that you can use parentheses () to ensure that the operations happen in the correct order. In this
case, the inner phrase (MiddleInitial + " ") will evaluate to the Middle Initial plus a space, or to null (if there is no middle initial). Then, the rest of the statement will be performed.

&和+之间分别,实际上只有string事件,在于您需要连接某事件string时,

寻找一个值。 如您在人的全名中寻找字节时。 代码如下,

strFullName = FirstName & ““& MiddleInitial &” “& LastName

您会发现一个小问题。 全名中间没有中间名(它在表里是空的)如Tom Smith的空间(space)

Tom Smith

幸好有另一置符: ”+”能弥补以上不足. 这置符解释是

“concatenation with null propagation.”最能在派对打动您们的朋友, "&"的作用更加容易解释是它连接两个string , 但,只有当两个字串的值。 如果二者之一为空的, 整体运算结果是空的。

+连接两个非Null字符串时的作用与&相同,但只要其中的一个字串为Null值,则连接结果就会是Null

使用以上例子, 最初如果没有中间,目标仅是一个空间分隔

名字和姓。 使用+符, 若中间名字不为空,我们可以另加:

MiddleName + " "

整件事例如下,

strFullName = FirstName & " " & (MiddleInitial + " ") & LastName

在MiddleInitial为Null值时,(MiddleInitial + " ")中的“+”避免了用“&”时在FirstName与LastName中出现两个空格的不合理结果。

在这个事例注意您能使用括号()按条件确保正确的运算结果。

在(MiddleInitial +  " ")内,将判断中间字符加上了空间, 或零位(如没有中间)。 然后, 其余声明将会执行。

TA的精华主题

TA的得分主题

发表于 2006-9-25 10:29 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

翻译的不太顺啊。

看得别扭!

TA的精华主题

TA的得分主题

发表于 2007-3-5 18:15 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2010-1-11 19:51 | 显示全部楼层
呵呵,那我来翻译一下吧。
总的来说是这样的,用+和&的效果一样,只不过当被符号连接的变量里有一个或多个变量是空值时,&后面的变量显示一个空格,+后面的变量干脆不显示。也就是用+可以避免有空变量时产生无用的空格。
翻译完毕。

[ 本帖最后由 slbjw 于 2010-1-11 19:52 编辑 ]
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-4 01:24 , Processed in 0.041598 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表