Posted By: Shark_M | Mar 12th, 2006 @ 8:52 PM
page 1 of 1
Comments: 12 | Views: 18275
   Hi guys,
The String.Replace (String1, string2) does not seem to be working.
I dont know why, but there might be some bug with it

Try something like this

Dim StrX as string = "@Testing"
now Dim StrY as string  = StrX.Replace ("@", String.Empty)


This will not work in my project, i dont know why

So i am forced to use the old function called Replace

so i say StrY = Replace (StrX,"@",String.Empty)

So why is the first one not working in VB.NET 2005?
Not sure what your problem is... the following works fine for me:

        Dim StrX As String = "@Testing"
        Dim StrY As String = StrX.Replace("@", String.Empty)
        System.Console.WriteLine(StrY)

The output is: "Testing"

I did the same in C# and it all worked fine. You may want to make sure you're running the 2.0 framework? Other than that, no clue.
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
I works fine here, in VB as well as C#, on .Net 2.0 RTM.
billwert
billwert
We can make it better, stronger, faster.

Same result here, unfortunately.  This works fine:

Dim foo As String = "@Testing"

Dim Bar As String = foo.Replace("@", String.Empty)

System.Console.WriteLine(Bar)

 

Tensor
Tensor
Im in yr house upgrading yr family

Bog standard VS 2005 install here, and it works fine.

 

 

Not working here(VB .NET 2.0 VS Proffesional)
thanks for the funtion  billwert
, old but works.




What MSIL is the compiler generating?
odujosh
odujosh
Need Microsoft SUX now!
If you are testing StringX for a change there will be none. StringY will have been changed though.

string StringX = "@Replace";
string StringY = StringX.Replace("@", string.Empty);
Console.WriteLine(StringX); //Results in @Replace
Console.WriteLine(StringY); //Results in Replace

If it s not a VB.NET problem. As string class is the same accross either language. My bet is some place you are calling stringX during  output when you should be calling StringY.

You can always do the replace to StringX if this is the case:
//C#:
StringX = StringX.Replace("@", string.Empty);
//VB.NET:
StringX = StringX.Replace("@", string.Empty)

I have used Replace scads of times. When I first learned of it I made the assignment error too. Assuming it would assign to itself. Being immutable the opposite makes total sense. Also being able to control where the result is stored makes a lot of sense.
This does not work for me either.
 
I am using VS 2003 with .NET 1.1

I think its something to do with the "@" character, b/c mine is not working and I am trying to replace "{"s  and  "}"s.


evildictaitor
evildictaitor
if( !succeed( try() ) ) { while(true) try(); }
It's nothing to do with what's in the string. It's to do with how string.replace works.

String String.Replace(String replaceString, String replaceWith)

This function does NOT change the string that calls the method - but instead returns a new string with the result:

String a = "@Testing";
String b = a.Replace("@", "blab");

// a == "@Testing", b == "blabtesting".
stevo_
stevo_
Human after all
I know this is an old thread, and that people have to learn.. but this is still pretty funny Tongue Out

In a 'shouldn't laugh but.. aw come on!' kinda way..

Dead give away about misunderstood mutability of strings in .net (or rather lack of) comes from the vbscript'esq global method 'Replace' vs the strings method..
page 1 of 1
Comments: 12 | Views: 18275
Microsoft Communities