0

Trasform a string from normal to camelcase

what's the best way to trasform "Hello World" into "HeLlO wOrLd" ? thanksđŸ–€

18th Feb 2022, 12:24 AM
Claudio Della Femina
Claudio Della Femina - avatar
4 Answers
+ 2
I guess you will need to work with char array. So you can go like this once you have a char array from the string Assuming first char is supposed to be in upper case let boolean upperCase = true for each character in char array if character is alphabetic if upperCase convert character to upper case else convert character to lower case end if let upperCase = NOT upperCase end if end for You can get help from `Character` class. It has methods needed to work with characters https://www.tutorialspoint.com/java/java_characters.htm
18th Feb 2022, 12:42 AM
Ipang
+ 1
"HeLlO wOrLd" is not camel case, it's more about case inversion. First char in upper case, second in lower case and so on. "helloWorld" is camel case "HelloWorld" is Pascal case
18th Feb 2022, 12:30 AM
Ipang
+ 1
Java String is immutable, you cannot transform it directly. You can use the StringBuilder class for that. https://zetcode.com/java/stringbuilder/ If you just want to print out the letters, without saving the string, then you don't even need that. Just loop through the letters, and depending on if the letter index is odd or even, print a lowercase or uppercase version of the character.
18th Feb 2022, 5:42 AM
Tibor Santa
Tibor Santa - avatar
0
oh, i didn't knew that sorry ahahaha... but how can i trasform only 1 character in the strong? using charAt()?
18th Feb 2022, 12:32 AM
Claudio Della Femina
Claudio Della Femina - avatar