force .Replace to act on itself

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

Dim s As String

s = ", ,, 1573 ,,"
s.Replace(",","").Replace(" ","")

doesn't give the desired result "1573" ... apparently because s.Replace()
returns a new String instance ...

so i figured this would work

s = s.Replace(",","").Replace(" ","")

but again, i don't get the desired result

i though
 
the following code which is just what you write works fine for me:
Dim s As String

s = ", ,, 1573 ,,"

s = s.Replace(",", "").Replace(" ", "")

MessageBox.Show("[" & s & "]")

Application.Exit()

You can use
s = s.Replace(",", "").Trim
 
isn't this one of thoes wierd cases with strings where you have to assign it
to a variable first then repeat it?
 
John,
Remember that strings are immutable (they don't change). All the functions
on String that change the string return a new instance of the string, hence
you need to assign the result to a variable.
s = s.Replace(",","").Replace(" ","")
but again, i don't get the desired result
What exactly is the desired result? After the above line s has just the
letters "1573"! What exactly are you expecting?

Hope this helps
Jay
 
* "John A Grandy said:
Dim s As String

s = ", ,, 1573 ,,"
s.Replace(",","").Replace(" ","")

doesn't give the desired result "1573" ... apparently because s.Replace()
returns a new String instance ...

so i figured this would work

s = s.Replace(",","").Replace(" ","")

but again, i don't get the desired result

The result of the code above is "1573", what would you expect?
 
Back
Top