r/shittyprogramming • u/ToastDroid • Nov 20 '18
How to Capitalize a String
word.ToCharArray()[0] = word.ToCharArray()[0].ToString().ToUpper().ToCharArray()[0];
69
Upvotes
r/shittyprogramming • u/ToastDroid • Nov 20 '18
word.ToCharArray()[0] = word.ToCharArray()[0].ToString().ToUpper().ToCharArray()[0];
View all comments
Show parent comment
2
u/TangibleLight Nov 21 '18 edited Nov 21 '18
And why not? Many languages support this, including JavaScript:
Along with Python, and Java.
In C/C++, with pointers, you can even assign to (mostly) arbitrary expressions, so long as the pointer types are correct:
In C# with ref returns, you can even assign directly to method calls:
You can use as complicated an expression as you want in the left-hand-side of the assignment, like in this (horrifying) Kotlin example:
No, the issue is that
toCharArrayreturns a new array every time. Assigning totoCharArray()[0]works just fine, but you lose the reference to that new array immediately, so it's lost. The way you get around that is by saving that new array as a variable first, then setting the value in that reference to the new array.It's got nothing to do with what's on the left-hand-side of the assignment.