为什么Replace返回Nothing ??? [英] Why does Replace return Nothing???

查看:74
本文介绍了为什么Replace返回Nothing ???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好 -


我有以下非常简单的代码...

Dim lStringA As String =""

Dim lStringB As String =""


....

lStringB = Replace(lStringA," ; DUMMY","",Compare:= CompareMethod.Text)

lStringB.TrimEnd(" \" c)


根据如果lStringA为零长度,则替换将返回零长度字符串

("")。那么为什么lStringB Nothing并且在调用TrimEnd()函数时导致

异常?


我是否必须检查lStringA是否是<> ""在我调用Replace()之前?


谢谢,

Joe

Hello -

I have the following very simple code ...

Dim lStringA As String = ""
Dim lStringB As String = ""

....

lStringB = Replace(lStringA, "DUMMY", "", Compare:=CompareMethod.Text)
lStringB.TrimEnd("\"c)

According to the documenation, Replace will return a zero-length string
("") if lStringA is zero-length. So why is lStringB Nothing and causes
an exception when calling the TrimEnd() function?

Do I have to check whether lStringA is <> "" before I call Replace()?

Thanks,
Joe

推荐答案

似乎文档是错误的(没有任何回报永远不会提到


http://msdn.microsoft.com/library/de .. .fctreplace.asp


除非有人解释没有发现查找的表达副本

作为可能的Nothing,无论如何不适用,因为没有什么是

即使非零长度替换也会返回。


我从未注意到这一点(我通常设法使用stringbuilders什么时候做?
替换......)


-tom

It does seem the documentation is wrong (a Nothing return is never
mentioned,

http://msdn.microsoft.com/library/de...fctreplace.asp

unless one interprets "Copy of Expression with no occurrences of Find"
as a possible Nothing, which anyway does not apply because Nothing is
returned even with a non zero-length "Replace" ).

I never noticed that (I usually manage to use stringbuilders when doing
replacements ... )

-tom


这是Microsoft.VisualBasic命名空间中的Replace函数如何实现:
实现:


公共共享函数替换(ByVal Expression As String,ByVal Find as

String,ByVal Replacement As String,ByVal Start as Integer = 1,ByVal Count

As Integer = -1,< OptionCompare> ByVal [Compare] As CompareMethod = 0)As

String

Dim text1 As String

试试

If(计数< -1)然后

扔新

ArgumentException(Utils.GetResourceString(" Argumen t_GEMinusOne1"," Count"))

结束如果

如果(开始< = 0)则

抛出新的
ArgumentException(Utils.GetResourceString(" Argumen t_GTZero1" ,, 开始))

结束如果

如果((表达式无)OrElse(开始> Expression.Length))那么

什么都不返回

结束如果

如果(开始<> 1)那么

Expression = Expression.Substring((Start - 1))

结束如果

If(((什么都没有)OrElse(Find.Length = 0))OrElse(Count = 0))

然后

返回表达式

结束如果

If(Count = -1)那么

Count = Expression.Length

结束如果

text1 = Strings.Join(Strings.Split(Expression,Find) ,(计数+ 1),

比较),替换)

捕获例外1作为例外

抛出异常1

结束尝试

返回文本1

结束函数


请注意,Expression参数定义为字符串,并且Start

参数默认为1.


在您的示例中,测试:


If((表达式无)OrElse(开始> Expression.Length))那么


将是真的,什么都不会被退回。如果空字符串被解释为
为Nothing,那么第一部分将为true,否则第二部分将为/ b $ b为真,因为1大于空字符串的长度。


所以我们剩下2个场景:


1 - 文档不正确


或者


2 - 无论谁编写了替换功能,都希望

什么都不会作为空字符串编组回来


无论哪种方式,行为都与文档不一致,是的,你需要在继续之前检查结果是否为Nothing。


如果您使用String.Replace方法,则可以避免这个问题。


" Joe HM" <未******* @ yahoo.com>在消息中写道

news:11 ********************* @ i39g2000cwa.googlegro ups.com ...
This is how the Replace function in the Microsoft.VisualBasic namespace is
implemented:

Public Shared Function Replace(ByVal Expression As String, ByVal Find As
String, ByVal Replacement As String, ByVal Start As Integer = 1, ByVal Count
As Integer = -1, <OptionCompare> ByVal [Compare] As CompareMethod = 0) As
String
Dim text1 As String
Try
If (Count < -1) Then
Throw New
ArgumentException(Utils.GetResourceString("Argumen t_GEMinusOne1", "Count"))
End If
If (Start <= 0) Then
Throw New
ArgumentException(Utils.GetResourceString("Argumen t_GTZero1", "Start"))
End If
If ((Expression Is Nothing) OrElse (Start > Expression.Length)) Then
Return Nothing
End If
If (Start <> 1) Then
Expression = Expression.Substring((Start - 1))
End If
If (((Find Is Nothing) OrElse (Find.Length = 0)) OrElse (Count = 0))
Then
Return Expression
End If
If (Count = -1) Then
Count = Expression.Length
End If
text1 = Strings.Join(Strings.Split(Expression, Find, (Count + 1),
Compare), Replacement)
Catch exception1 As Exception
Throw exception1
End Try
Return text1
End Function

Note that the Expression parameter is defined as string and that the Start
parameter defaults to 1.

In your example, the test:

If ((Expression Is Nothing) OrElse (Start > Expression.Length)) Then

will be true and Nothing will be returned. If an empty string is interpreted
as Nothing then the first part will be true otherwise the second part will
be true because 1 is greater than the length of an empty string.

So we are left with 2 scenarios:

1 - the documentation is incorrect

or

2 - whoever wrote the Replace function expects that
Nothing will be marshalled back as an empty string

Either way, the behaviour is at odds with the documentation and, yes, you
should either check to see if the result is Nothing before continuing.

If you were to use the String.Replace method instead you would avoid this
issue.

"Joe HM" <un*******@yahoo.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
你好 -

我有以下非常简单的代码...

Dim lStringA As String =""
Dim lStringB As String =" ;

...

lStringB =替换(lStringA," DUMMY","",Compare:= CompareMethod.Text)
lStringB .TrimEnd(" \" c)

根据文档,如果lStringA为零长度,则Replace将返回零长度字符串
(")。那么为什么lStringB Nothing并且在调用TrimEnd()函数时会导致异常?

我是否必须检查lStringA是否是<> ""在我调用Replace()之前?

谢谢,
Joe
Hello -

I have the following very simple code ...

Dim lStringA As String = ""
Dim lStringB As String = ""

...

lStringB = Replace(lStringA, "DUMMY", "", Compare:=CompareMethod.Text)
lStringB.TrimEnd("\"c)

According to the documenation, Replace will return a zero-length string
("") if lStringA is zero-length. So why is lStringB Nothing and causes
an exception when calling the TrimEnd() function?

Do I have to check whether lStringA is <> "" before I call Replace()?

Thanks,
Joe





你好斯蒂芬妮,有趣......你从哪里得到的? :)


-tom

PS

"如果空字符串被解释为Nothing

不符合我的经验(string.empty不是什么都没有)


Hi Stephany , interesting... where did you get that ? :)

-tom
PS
"If an empty string is interpreted as Nothing"
that does not correspond to my experience (string.empty is not nothing)


这篇关于为什么Replace返回Nothing ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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