格式化文本字段 [英] Format a text field

查看:87
本文介绍了格式化文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我正在学习格式,但它是否可以使用字符串?


我想取这个字符串

dim thisCustNo as string =" ; 9128764526"

并让它看起来像

(912)876-4526


我发现我能做到这一点

thisCustNo.Format("({0}){1} - {2}",thisCustNo.Substring(0,3),thisCustNo.Substrin g(3,3),thisCustNo.Substring( 6,4))


"(" + thisCustNo.Substring()更简单,更清晰或更好。 0,3)+")" + thisCustNo.Subst ring(3,3)+" - " + thisCustNo.Substring(6,4)


我错过了什么?

While I''m learning about format, will it work with strings?

I want to take this string
dim thisCustNo as string = "9128764526"
and make it look like
(912)876-4526

I found I can do this
thisCustNo.Format("({0}){1}-{2}",thisCustNo.Substring(0,3),thisCustNo.Substrin g(3,3),thisCustNo.Substring(6,4))

But for me that isn''t a bit easier or clearer or nicer than
"("+thisCustNo.Substring(0,3)+")"+thisCustNo.Subst ring(3,3)+"-"+thisCustNo.Substring(6,4)

Am I missing something?

推荐答案

使用哪个更容易。你的第二个例子是正确的,除了

使用+而不是& ;.你也可以检查正则表达式。


" cj2" < cj*@nospam.nospamwrote in message

news:%2 **************** @ TK2MSFTNGP04.phx.gbl ...
Use whichever seems easier to you. Your second example is correct except for
the use of + instead of &. You could also examine regular expressions.

"cj2" <cj*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...

虽然我正在学习格式,但它是否可以使用字符串?


我想取这个字符串

dim thisCustNo as string =" 9128764526"

并让它看起来像

(912)876-4526


我发现我可以这样做

thisCustNo.Format("({0}){1} - {2}",thisCustNo.Substring(0,3),thisCustNo.Substrin g (3,3),thisCustNo.Substring(6,4))


更容易,更清晰或更好"(" + thisCustNo.Substring(0,3)+")" + thisCustNo.Subst ring(3,3)+" - " + thisCustNo.Substring(6,4)


我错过了什么吗?
While I''m learning about format, will it work with strings?

I want to take this string
dim thisCustNo as string = "9128764526"
and make it look like
(912)876-4526

I found I can do this
thisCustNo.Format("({0}){1}-{2}",thisCustNo.Substring(0,3),thisCustNo.Substrin g(3,3),thisCustNo.Substring(6,4))

But for me that isn''t a bit easier or clearer or nicer than
"("+thisCustNo.Substring(0,3)+")"+thisCustNo.Subst ring(3,3)+"-"+thisCustNo.Substring(6,4)

Am I missing something?


你好CJ,


两种解决方案都应该正常工作。然而,就像詹姆斯指出的那样,在第二个解决方案中,我们最好使用&。字符串的运算符

连接。 " + QUOT;可能会导致意外的结果,例如,它可能会将字符串解析成数字用于算术运算而不是字符串

操作。


谢谢。


祝你好运,

Jeffrey Tan

Microsoft在线社区支持

== =======================================

让客户满意是我们的首要任务。我们欢迎您的意见和

有关我们如何改进我们为您提供的支持的建议。请

随时让我的经理知道您对服务水平的看法

提供。您可以直接向我的经理发送反馈:
ms****@microsoft.com


此帖子按原样提供。没有保证,也没有赋予任何权利。

Hi CJ,

Both solutions should work correctly. However, just as James pointed out,
in the second solution, we''d better use "&" operator for string
concatenation. "+" may cause unexpected result, for example, it may parse
the string into number for arithmetic operation instead of string
operation.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.


Jeffrey,


我同意当然完成,它是更好的显示差异

之间的字符串连接和数学运算之间的这种情况在VB中是可能的。


但是,计数行为仅适用于选项严格关闭。


无需回复,这是OP,我非常清楚你知道的。


: - )


Cor

"" Jeffrey Tan [MSFT]"" < je *** @ online.microsoft.comschreef在bericht

新闻:ml ************** @ TK2MSFTNGHUB02.phx.gbl ...
Jeffrey,

I agree of course complete and it is much nicer to show the difference
between a string concat and a math operation like that is in VB possible.

However, the counting behaviour is only with Option Strict Off.

No reply needed, it is for the OP, I know very well that you know that.

:-)

Cor
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comschreef in bericht
news:ml**************@TK2MSFTNGHUB02.phx.gbl...

嗨CJ,


两种解决方案都应该正常工作。然而,就像詹姆斯指出的那样,在第二个解决方案中,我们最好使用&。字符串的运算符

连接。 " + QUOT;可能会导致意外的结果,例如,它可能会将字符串解析成数字用于算术运算而不是字符串

操作。


谢谢。


祝你好运,

Jeffrey Tan

Microsoft在线社区支持

== =======================================

让客户满意是我们的首要任务。我们欢迎您的意见和

有关我们如何改进我们为您提供的支持的建议。请

随时让我的经理知道您对服务水平的看法

提供。您可以直接向我的经理发送反馈:
ms****@microsoft.com


此帖子按原样提供。没有保证,也没有赋予

权利。
Hi CJ,

Both solutions should work correctly. However, just as James pointed out,
in the second solution, we''d better use "&" operator for string
concatenation. "+" may cause unexpected result, for example, it may parse
the string into number for arithmetic operation instead of string
operation.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.


这篇关于格式化文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆