C
C#2w ago
Moon

string x = "6";x = x.Replace("6", "4");Console.WriteLine(x);

'why cant i use int here? thanks for the help btw
7 Replies
Pobiega
Pobiega2w ago
The question doesn't make sense.
Moon
MoonOP2w ago
wdym
Pobiega
Pobiega2w ago
Because you can't do string replacement in an integer? and int.Replace doesn't make sense if you understand what an int is Strings hold text, and it does make sense to replace all instances of some character or substring with another while int holds whole numbers, and its not really a common thing in math to just straight up replace all "5"s in a number with another number What you can do if thats strictly needed is to turn your int into a string, do string replacement, then parse it back into an int
Moon
MoonOP2w ago
i got it thanks bro
Angius
Angius2w ago
Every string consists of chars. What is important, is that a char for a given number is not that number. For example, char code for '9' is 57 Since it's the 57th character on the ASCII table And a string like "69" is char '6' (code 54) and char '9' (code 57)
MODiX
MODiX2w ago
Mayor McCheese
REPL Result: Success
6-2
6-2
Result: int
4
4
Compile: 160.124ms | Execution: 14.096ms | React with ❌ to remove this embed.
Angius
Angius2w ago
You can do arithmetics on chars, yes, and usually you will get the correct result. Both 9 - 6 and 57 - 54 result in 3 after all

Did you find this page helpful?