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/TheTrueSwishyFishy Nov 20 '18 edited Nov 21 '18
The code is not syntactically valid, word.toCharArray() gets a value that can be assigned to a variable, not a variable that can be changed; it is essentially equivalent to 1 = 2 or ‘a’ = ‘A’ or even SomeObject.getHeight() = 10
Something like
char[] chars = word.toCharArray(); chars[0] = chars[0].toString().toUpperCase(); word = String.valueOf(chars);
Would work
Edit: I seem to not know what I am talking about