2

I want to convert a string of the form ACT_PACKAGE_UTILITY to actPackageUtility but using Java 8. It's a piece of cake using Scala, for example

 def underscoreToCamel(name: String) = "_([a-z\\d])".r.replaceAllIn  

   (name,{m => m.group(1).toUpperCase() 
 }) 

However, this seems quite complex in Java because the replaceAll in String does not accept a function.

Here is my attempt to get the values to insert the uppercase characters but I'm stuck.

    Pattern pattern = Pattern.compile("_([a-z\\d])");
    Matcher matcher = pattern.matcher(event.toLowerCase());
    String evey=event.toLowerCase();

    while(matcher.find()){
    evey=evey.replace(evey.charAt(indexOf(matcher.group(1)),,    
                  Character.toUpperCase(matcher.group(1).toCharArray()[0]);
       }

Is there away to achieve it with Java 8?

BreenDeen
  • 623
  • 1
  • 13
  • 42
  • 1
    If you can use guava: `CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "ACT_PACKAGE_UTILITY")` – sergey Nov 15 '17 at 13:35
  • Thanks Sergey, I used the guava solution! My preference was to use a Java 8 functional approach i.e.. using a pipeline. However, I don't want to spend any more time on it! Cheers! – BreenDeen Nov 15 '17 at 15:27
  • Wiktor, plain text conversion with no undercores is not the same question. Besides if you had taken the time to read the question properly without jumping to conclusions you would have found out I was looking for a functional approach to the solution. It clearly states Java 8. Look before you leap! The operative question is 'Is there a way to solve it using Java 8?' – BreenDeen Nov 15 '17 at 15:29

0 Answers0