Change "." to "_" in a C # line

I am developing a piece of software in C #, and the end result is an Excel spreadsheet. A spreadsheet header is created using several variables to accurately explain what a spreadsheet is. One of the variables is a string that contains such data:

'1.1.1'

I need to convert it at the time of creating the table:

'1_1_1

I tried to use the method String.Replace, but it just ignores it. Any ideas?

Regards

+3
source share
7 answers

I bet you do this:

myString.Replace(".","_");

When you should do this:

myString = myString.Replace(".","_");

Remember that .Net strings are immutable, so any changes result in a new string.

+35

, string.Replace. :

text = text.Replace('.', '_');

Replace - . .NET - .

+12

string.Replace, , ?

yourString.Replace(".", "_");

.

string newString = yourString.Replace(".", "_");

.

+4

, , String.Replace. , String.Replace , .

string foo = "1.1.1";
foo = foo.Replace('.', '_');
+2
String input = "1.1.1";
input = input.Replace(".", "_");
+1

, , :

string myString = "1.1.1";
myString = myString.Replace('.', '_');
0

String.Replace - :

 private void button1_Click(object sender, RoutedEventArgs e) {
        String myNumbers = "1.1.1";
        Console.WriteLine("after replace: " + myNumbers);
        myNumbers = myNumbers.Replace(".", "_");
        Console.WriteLine("after replace: " + myNumbers);
    }

:

after replace: 1.1.1
after replace: 1_1_1
0

All Articles