I'm a very beginner to C# and following some tutorials. I got this issue on manipulating strings recently.
string myName = "Jhon";
myName = myName.ToUpper();
myName = myName.Replace("Jhon", "doe").ToUpper();
Console.WriteLine($"Hello {myName}");
Console.WriteLine($"Hello {myName}");
and the answer was
Hello JHON
Hello JHON
Why the string Jhon did not get replaced with doe ? and why the first one worked and the last one didn't ?
CodePudding user response:
You change the value of myName
to upper cases with the function in the second line .ToUpper()
, transforming Jhon to JHON.
Then on the third line you try to replace Jhon by doe, but don't forget that you just transformed Jhon to JHON!
The .Replace()
function is case sensitive and will not find anything matching to replace in your string
Also, if you want to display myName
just after transforming it to upper cases, you should place a Console.WriteLine($"Hello {myName}");
between your second and third line, because if not you will modify again the value before even displaying it in the console
At the end your code could look like this :
string myName = "Jhon";
myName = myName.ToUpper();
Console.WriteLine($"Hello {myName}");
myName = myName.Replace("JHON", "doe").ToUpper();
Console.WriteLine($"Hello {myName}");
CodePudding user response:
After myName = myName.ToUpper();
myName variable is equal to "JHON" and strings are case sensitive for Replace() method.
CodePudding user response:
Why the string Jhon did not get replaced with doe?
Because there was JHON
value instead of Jhon
due to ToUpper()
call and assignement.
You probably wanted to do something like this
string myName = "Jhon";
string myName1 = myName.ToUpper();
string myName2 = myName.Replace("Jhon", "doe").ToUpper();
Console.WriteLine($"Hello {myName1}");
Console.WriteLine($"Hello {myName2}");
CodePudding user response:
First. Strings by their very nature are immutable in C#. That means you can never really change existing string. You can simply create a new string based on old one.
Secondly: Please google a bit about passing by value vs passing be reference. String are the type that get's passed by reference. This means that myName is not a block of characters, it's a reference to it.
Thing about it like about a dude whom you can ask where something is. The dude name is myName and first you tell him that he should point to the "Jhon" text. Than you ask computer to write a new string in a different place with Jhon being in uppercase then tell dude myName that from now on he should point you to that new string.
Than finally you ask myName to led you to the string it's pointing to (after previous step) it's pointing to value "JOHN" than you ask computer to replace every occurrence of "John" inside "JOHN" to "doe" since Replace is case sensitive it does not find any occurrence of "John" inside "JOHN" therefore you get your final result.
It's really good idea to print variable values after each operation so you will actually know which operation is not working like expected.