trim(string)vs string.trim [英] trim(string) vs string.trim

查看:97
本文介绍了trim(string)vs string.trim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根据字符串内容做出决定的应用。我需要确保字符串不包含空格或换行符。

。我使用的是

语法''Trim(String)"它工作正常。我以为我会把它改成VB

..NET方法" String.Trim"但这引发了一个对象异常。


这带来了一个问题:它是否符合使用Trim(字符串),或者它是否更符合礼仪使用
如果Not String Is Nothing那么String.Trim?

I have an app that makes decisions based on string content. I need to make
sure that a string does not contain only spaces or newlines. I am using the
syntax ''Trim(String)" and it works fine. I thought I''d change it to the VB
..NET method "String.Trim" but that throws an object exception.

Which brings the question: is it compliant to use Trim(String), or is it
more within etiquette to use If Not String Is Nothing Then String.Trim?

推荐答案

Terry,

你应该经常检查你的参数/变量null引用

每当有疑问时。


如果你愿意,可以使用否定逻辑,它只是不那么直接

前进(就在这里,我只是尽可能地避免否定逻辑)

如果不是没有那么

stringVariable = stringVariable.Trim

结束如果


如果stringVariable什么都没有那么

stringVariable = string.empty

否则

stringVariable = stringVariable .Trim

结束如果


无论你决定什么,只需要有意义。


" Terry Olsen"写道:
Terry,
You should always check your parameters/variables for null references
whenever there is doubt.

You can use the negating logic if you prefer, its just not as straight
forward (it is here, I just avoid negating logic whenever possible)
If Not Is Nothing Then
stringVariable = stringVariable.Trim
End If

If stringVariable is nothing then
stringVariable = string.empty
Else
stringVariable = stringVariable.Trim
End If

Whatever you decide, just be consistient.

"Terry Olsen" wrote:

我有一个应用程序,根据字符串内容做出决定。我需要确保字符串不包含空格或换行符。

。我使用的是

语法''Trim(String)"它工作正常。我以为我会把它改成VB

..NET方法" String.Trim"但这引发了一个对象异常。


这带来了一个问题:它是否符合使用Trim(字符串),或者它是否更符合礼仪使用
如果Not String Is Nothing Then String.Trim?
I have an app that makes decisions based on string content. I need to make
sure that a string does not contain only spaces or newlines. I am using the
syntax ''Trim(String)" and it works fine. I thought I''d change it to the VB
..NET method "String.Trim" but that throws an object exception.

Which brings the question: is it compliant to use Trim(String), or is it
more within etiquette to use If Not String Is Nothing Then String.Trim?


不要忘记2.0中引入的新IsNot运算符。这有点

更容易阅读imo。


如果stringVariable IsNot Nothing那么

stringVariable = stringVariable.Trim

结束如果


/ claes


" Jared" < Ja *** @ discuss.microsoft.com写信息

news:24 ************************* ********* @ microsof t.com ...
Don''t forget the new IsNot operator that was introduced in 2.0. It''s a bit
easier to read imo.

If stringVariable IsNot Nothing Then
stringVariable = stringVariable.Trim
End If

/claes

"Jared" <Ja***@discussions.microsoft.comwrote in message
news:24**********************************@microsof t.com...

特里,

你应该经常检查你的参数/空引用的变量

每当有疑问时。


如果你愿意,可以使用否定逻辑,它只是不那么直接

forward(就在这里,我只是尽可能地避免否定逻辑)

如果不是什么然后

stringVariable = stringVariable.Trim

结束如果


如果stringVariable什么都没有那么

stringVariable = string.empty

否则

stringVariable = stringVariable.Trim

结束如果


无论你决定什么,只需要有意义。


Terry Olsen ;写道:
Terry,
You should always check your parameters/variables for null references
whenever there is doubt.

You can use the negating logic if you prefer, its just not as straight
forward (it is here, I just avoid negating logic whenever possible)
If Not Is Nothing Then
stringVariable = stringVariable.Trim
End If

If stringVariable is nothing then
stringVariable = string.empty
Else
stringVariable = stringVariable.Trim
End If

Whatever you decide, just be consistient.

"Terry Olsen" wrote:

>我有一个应用程序,根据字符串内容做出决定。我需要确保字符串不仅包含空格或换行符。我正在使用
语法''Trim(String)"它工作正常。我以为我会把它改成
VB
..NET方法String.Trim但这引发了一个对象异常。

这带来了一个问题:它是否符合使用Trim(字符串),或者它是否更符合礼仪使用If Not String Is Nothing Then String。修剪?
>I have an app that makes decisions based on string content. I need to
make
sure that a string does not contain only spaces or newlines. I am using
the
syntax ''Trim(String)" and it works fine. I thought I''d change it to the
VB
..NET method "String.Trim" but that throws an object exception.

Which brings the question: is it compliant to use Trim(String), or is it
more within etiquette to use If Not String Is Nothing Then String.Trim?



Claes,
Claes,

Don''忘记2.0中引入的新IsNot运算符。这有点

更容易阅读imo。
Don''t forget the new IsNot operator that was introduced in 2.0. It''s a bit
easier to read imo.



也许对于英国本土人来说,我觉得阅读起来很糟糕。


Cor


" Claes Bergefall" < lo ***** @ nospam.nospamschreef in bericht

新闻:%2 **************** @ TK2MSFTNGP02.phx.gbl .. 。

Maybe for native English people, I find it terrible to read.

Cor

"Claes Bergefall" <lo*****@nospam.nospamschreef in bericht
news:%2****************@TK2MSFTNGP02.phx.gbl...


别忘了2.0中引入的新IsNot运算符。这有点

更容易阅读imo。


如果stringVariable IsNot Nothing那么

stringVariable = stringVariable.Trim

结束如果


/ claes


" Jared" < Ja *** @ discuss.microsoft.com写信息

news:24 ************************* ********* @ microsof t.com ...
Don''t forget the new IsNot operator that was introduced in 2.0. It''s a bit
easier to read imo.

If stringVariable IsNot Nothing Then
stringVariable = stringVariable.Trim
End If

/claes

"Jared" <Ja***@discussions.microsoft.comwrote in message
news:24**********************************@microsof t.com...

> Terry,
你应该经常检查你的参数/变量只要有疑问,就可以使用否定逻辑。

如果你愿意,你可以使用否定逻辑,它只是不那么直接(它在这里,我只是避免否定逻辑可能)
如果不是什么呢
stringVariable = stringVariable.Trim
结束如果

如果stringVariable什么都没有那么
stringVariable = string.empty
否则
stringVariable = stringVariable.Trim
结束如果

无论你决定什么,只需要有意义。

Terry Olsen写道:
>Terry,
You should always check your parameters/variables for null references
whenever there is doubt.

You can use the negating logic if you prefer, its just not as straight
forward (it is here, I just avoid negating logic whenever possible)
If Not Is Nothing Then
stringVariable = stringVariable.Trim
End If

If stringVariable is nothing then
stringVariable = string.empty
Else
stringVariable = stringVariable.Trim
End If

Whatever you decide, just be consistient.

"Terry Olsen" wrote:

>>我有一个应用程序,根据字符串内容做出决定。我需要确保字符串不仅包含空格或换行符。我正在使用
语法''Trim(String)"它工作正常。我以为我会把它改成
VB
..NET方法String.Trim但这引发了一个对象异常。

这带来了一个问题:它是否符合使用Trim(字符串),或者它是否更符合礼仪使用If Not String Is Nothing Then String。修剪?
>>I have an app that makes decisions based on string content. I need to
make
sure that a string does not contain only spaces or newlines. I am using
the
syntax ''Trim(String)" and it works fine. I thought I''d change it to the
VB
..NET method "String.Trim" but that throws an object exception.

Which brings the question: is it compliant to use Trim(String), or is it
more within etiquette to use If Not String Is Nothing Then String.Trim?




这篇关于trim(string)vs string.trim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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