473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Why does Replace return Nothing???

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

Apr 12 '06 #1
11 4275
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

Apr 12 '06 #2
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...
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

Apr 13 '06 #3

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)

Apr 13 '06 #4
I had a look at it in Lutz Roeder's Reflector.

http://www.aisto.com/roeder/dotnet/

It is excellent for delving into the Framwork and seeing how things are
implemented.

Yes. You're right. I was wondering out loud if there might or might not be
some mechanism in the framework that somehow modified the values that are
marshalled to and from the methods (functions) in Microsoft.VisualBasic
namespace, so that an empty string got interpreted as Nothing an/or vice
versa. You may or may not recall that back in VB6 an number of the string
functions returned variant containg strings rather than true strings. It was
probably a spurious thought but it did cross my mind.
<to**************@uniroma1.it> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...

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)

Apr 13 '06 #5
Sephany,

Very interesting and really odd what you found. I hope on a comment from
Herfried, who loves the VB replace and methods like that.

Cor
"Stephany Young" <noone@localhost> schreef in bericht
news:u7**************@TK2MSFTNGP05.phx.gbl...
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...
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


Apr 13 '06 #6
Hello -

Wow ... thanks for all the feedback! I have looked into using the
String.Replace function but I cannot set the
Compare:=CompareMethod.Text (case-insensitive) that way ... can I?

Thanks,
Joe

Apr 13 '06 #7
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
Very interesting and really odd what you found. I hope on a comment from
Herfried, who loves the VB replace and methods like that.


The documentation for 'Microsoft.VisualBasic.Strings.Replace' in .NET 2.0
states:

| *Return Value*
| [...]
| 'Nothing' Expression is zero-length or 'Nothing', or 'Start' is
| greater than length of 'Expression'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Apr 13 '06 #8
You are so right Herfried.

The link a poster gave earlier in the thread actually refers to .Net 1.0/1.1
so this is a change in .NET 2.0.
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eN**************@TK2MSFTNGP03.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
Very interesting and really odd what you found. I hope on a comment from
Herfried, who loves the VB replace and methods like that.


The documentation for 'Microsoft.VisualBasic.Strings.Replace' in .NET 2.0
states:

| *Return Value*
| [...]
| 'Nothing' Expression is zero-length or 'Nothing', or 'Start' is
| greater than length of 'Expression'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Apr 13 '06 #9
Stephany,

"Stephany Young" <noone@localhost> schrieb:
You are so right Herfried.

The link a poster gave earlier in the thread actually refers to .Net
1.0/1.1 so this is a change in .NET 2.0.


You are right, the documentation on .NET 1.0/1.1's 'Replace' function
doesn't state this case. Maybe it was a bug in the implementation and
Microsoft decided to change the documentation to solve the problem.
Personally I would have changed the implementation of the method to return
an empty string instead of a 'Nothing' reference, because it would seem more
intuitive to me, but I didn't spend time on thinking about the implications
of such a change.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Apr 13 '06 #10
I agree Herfried. Returning an empty string would certainly be more
intuitive from my point of view. Also, IIRC, the empty string has been the
return value since Replace was introduced to VB (can't remember whether it
was VB5 or VB6).
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Stephany,

"Stephany Young" <noone@localhost> schrieb:
You are so right Herfried.

The link a poster gave earlier in the thread actually refers to .Net
1.0/1.1 so this is a change in .NET 2.0.


You are right, the documentation on .NET 1.0/1.1's 'Replace' function
doesn't state this case. Maybe it was a bug in the implementation and
Microsoft decided to change the documentation to solve the problem.
Personally I would have changed the implementation of the method to return
an empty string instead of a 'Nothing' reference, because it would seem
more intuitive to me, but I didn't spend time on thinking about the
implications of such a change.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Apr 13 '06 #11
Yeah ... I have NET 1.0/1.1 ... so I guess the documentation is not
right ...

Thanks!
Joe

Apr 13 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid =...
6
by: WindAndWaves | last post by:
Hi Folks I have inhereted a script that I understand reasonably well, I just do not understand !/^\d+$/.test(el.value) what the hell does that mean? Below is the script (there are really...
4
by: grocery_stocker | last post by:
#include <iostream> //for cin and cout #include <iomanip> // for setw() #include <string> // for strlen() strcmp() strrev() #include <fstream> //ifstream and ofstream: file (input & output)...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
1
by: Harry F. Harrison | last post by:
In VS 2003, If an empty string is passed into the 1st parameter, the Replace function returns Nothing. Example: Dim strResults As String = "" strResults = Microsoft.VisualBasic.Replace("",...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
1
by: braj | last post by:
Hi Friends, I was trying a test code to use the icu stuff with variable arguments and I seem to be heading nowhere. Can anyone please help. Given below is the test code and the Output. ...
3
by: Sorrow | last post by:
I was watching a javascript presentation and one of the slides had the following code: <code> String.prototype.supplant = function( o ) { return this.replace(/{(*)}/g, function( a, b ) { var...
3
by: Sin Jeong-hun | last post by:
If I use something like this, string html = "<h1>C# is great</h1>"; Console.WriteLine(html.Replace("&lt;","<").Replace("&gt;",">")); Does this recreate new string objects two times, even though it...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.