📜 ⬆️ ⬇️

Universal toCamelCase () function for Java

Today I needed to translate a string of arbitrary content into camelCase.
On the Internet, narrowly specialized methods were mostly encountered, which either translate only constant names (according to Java conventions, SOME_JAVA_NAMING_CONVENTION_CONST), or only phrases separated by spaces.
I categorically lacked this, I needed more versatility.

As befits any self-respecting cyclist, I started writing my own algorithm, and got a little carried away. Waking up from the code, I found that the function translates any fed-up strings to normal camelCase or CamelCase.
The only thing that it does not do is not prohibit the numbers at the beginning of the resulting line (for the JNC agreements), but I didn’t need it (if necessary, append it with one line of code - adding to the second, embedded condition).

What happened can be seen under the cut.

')
The function takes two arguments - the actual string, and the flag indicating to write the result with a capital letter (under-camelCase).
She does it all in one pass. The code is sprinkled with comments for beginners (to whom this cheat sheet is written).
If you pity the second argument, you can bite off painlessly.
Also, the algorithm is practically unchanged ported to javascript and C #.

/** *     camelCase ( CamelCase) . * * @param string   * @param firstWordToLowerCase        (lowercase). */ public static String toCamelCase(String string, boolean firstWordToLowerCase) { char currentChar, previousChar = '\u0000'; //      StringBuilder result = new StringBuilder(); //       boolean firstLetterArrived = !firstWordToLowerCase; // ,        lowercase boolean nextLetterInUpperCase = true; // ,       UPPERCASE //       for (int i = 0; i < string.length(); i++) { currentChar = string.charAt(i); /*      -      (  )     .        ,      -      (  ). */ if (!Character.isLetterOrDigit(currentChar) || ( ((Character.isLetter(previousChar) && Character.isLowerCase(previousChar)) || Character.isDigit(previousChar)) && Character.isLetter(currentChar) && Character.isUpperCase(currentChar)) ) { nextLetterInUpperCase = true; if (!Character.isLetterOrDigit(currentChar)) { previousChar = currentChar; continue; } } //     ,     . if (nextLetterInUpperCase && firstLetterArrived) { result.append(Character.toUpperCase(currentChar)); } else { result.append(Character.toLowerCase(currentChar)); } //  . firstLetterArrived = true; nextLetterInUpperCase = false; previousChar = currentChar; } //   . return result.toString(); } 


Well, examples of the results of the function
Source string: 'normalCamelCaseName'
Result string: 'normalCamelCaseName'
Result string: 'NormalCamelCaseName' (firstWordToLowerCase = false)
===========================
Source string: 'NotCamelCaseName'
Result string: 'notCamelCaseName'
Result string: 'NotCamelCaseName' (firstWordToLowerCase = false)
===========================
Source string: 'CONSTANT_TO_CAMEL_CASE'
Result string: 'constantToCamelCase'
Result string: 'ConstantToCamelCase' (firstWordToLowerCase = false)
===========================
Source string: 'Text To Camel Case'
Result string: 'textToCamelCase'
Result string: 'TextToCamelCase' (firstWordToLowerCase = false)
===========================
Source string: 'Text to camel case'
Result string: 'textToCamelCase'
Result string: 'TextToCamelCase' (firstWordToLowerCase = false)
===========================
Source string: 'LIFE ON SHIFT, DRUJEFFKI! :)'
Result string: 'SEARCH EFFORTING YOURSELF TRAINS'
Result string: 'SEARCH EFFORT ON YOUR IMPROVEMENTS' (firstWordToLowerCase = false)
===========================
Source string: '- (* & * &% &% $ ^ & ^ * () Characters * & ^% * (& $ punctuation ... and. Unreadable ----------- characters ^ (Maybe * 90Beat (*? * ?: HOW MANY *?%?:% Please! '
Result string: 'charactersButtonBircuitsBoxedChangsMaybe90BeTlichnoeVyhodno'
Result string: 'SignsKits of ReadableSignsMaybe90BeSkolkoSyagno' (firstWordToLowerCase = false)
===========================
Source string: 'And, finally, a Russian string with punctuation marks (localization!).'
Result string: 'and Finally, the Russian StringConsChangingKitsLocalization'
Result string: 'Into the endRussianStringTimeChapterChutingsLocalization' (firstWordToLowerCase = false)

Source: https://habr.com/ru/post/270425/


All Articles