Replace problem

  • Thread starter Thread starter JJ
  • Start date Start date
J

JJ

Hi all,

I've got a very strange problem: I want to convert the commas in strings to
dots. This is the function I wrote for this action:

private string ddot(double input){
string str = Convert.ToString(input);
str.Replace(",", ".");
return str;
}

For some reason, this function returns a string including a comma in place
of a dot.

Thanks in advance,

Vincent
 
JJ said:
I've got a very strange problem: I want to convert the commas in strings to
dots. This is the function I wrote for this action:

private string ddot(double input){
string str = Convert.ToString(input);
str.Replace(",", ".");
return str;
}

For some reason, this function returns a string including a comma in place
of a dot.

It's because str.Replace doesn't change the string it's called on (no
methods do this; strings are immutable). Instead, it returns a string
with the comma converted to a dot - but you're ignoring that return
value.
 
Hi,

a string is an inmutable type, meaning that once it's created it CANNOT be
modified, all the methods that could modify it returns a new instance of
String.

you have to code it like this
str = str.Replace(",", ".");

Read this (taken from MSDN):
A string is a sequential collection of Unicode characters, typically used to
represent text, while a String is a sequential collection of System.Char
objects that represents a string. The value of the String is the content of
the sequential collection, and the value is immutable.

A String is called immutable because its value cannot be modified once it
has been created. Methods that appear to modify a String actually return a
new String containing the modification. If it is necessary to modify the
actual contents of a string-like object, use the System.Text.StringBuilder
class.


Cheers,
 
C Addison Ritchie said:
The first problem I see is in the method signature. A double
converted to a string will never have a comma in it because I can't
assign a double to something like "3, 456.00".

That depends on your default culture. Try this:

using System;
using System.Globalization;
using System.Threading;

public class Test
{
static void Main()
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("fr-FR");

double d = 0.5;
Console.WriteLine (d.ToString());
}
}
 
Course in this scenario it would be taking something that is a period already, turn it into a comma only to turn it back into a period.

Perhaps the real question to ask JJ is why he needs to do this. If he is working with globalization issues then maybe he (and I) should read up on globalization and see if the .NET Framework can help him.
 
C Addison Ritchie said:
Ah... the French can eat my shorts. ;-)

Anyone with "C" and "Ritchie" in their name must get two programmer points
by osmosis...
And +1 on le short-eating of the French. :)
 
C Addison Ritchie said:
Course in this scenario it would be taking something that is a period
already, turn it into a comma only to turn it back into a period.

In what way does it turn anything into a comma? A double doesn't have a
dot any more than it has a comma - it's just a number. The fact that in
many cultures the decimal point is represented by a dot doesn't mean
that something formatting it has to start with that and then turn it
into a comma.
 
Back
Top