string x = "6";x = x.Replace("6", "4");Console.WriteLine(x);
'why cant i use int here? thanks for the help btw
7 Replies
The question doesn't make sense.
wdym
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 inti got it
thanks bro
Every
string
consists of char
s. 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
)Mayor McCheese
REPL Result: Success
Result: int
Compile: 160.124ms | Execution: 14.096ms | React with ❌ to remove this embed.
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