1

I tried as the below code code snippet, but the TradeID is printing as Trade_I_D, but it must be as Trade_ID.

  • input: getCurrency, getAccountName, getTradeID
  • expected output: Currency, Account_Name, Trade_ID
public class RemoveGet {

    public static void main(String args[]) {
        for (String a : args) {
            String b = a.replace("get", "");
            //System.out.println(b);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < b.length(); i++) {
                if (Character.isUpperCase(b.charAt(i))) {
                    sb.append("_");
                    sb.append(b.charAt(i));
                } else {
                    sb.append(b.charAt(i));
                }
            }
            //System.out.println(sb.toString());
            String c = sb.toString();
            if (c.startsWith("_")) {
                System.out.println(c.substring(1));
            }
        }
    }
}

Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
Sai Chand
  • 11
  • 1
  • Just saying: when following java naming conventions, the correct camel case would be TradId. Which looks weird, so I get it why you prefer ID instead of Id. – GhostCat Apr 19 '22 at 13:34
  • @GhostCat I think it should be `TradeId`, which looks perfectly fine to me :-) – MC Emperor Apr 19 '22 at 14:26
  • Does this answer your question? [Regex for converting CamelCase to camel\_case in java](https://stackoverflow.com/questions/10310321/regex-for-converting-camelcase-to-camel-case-in-java) – TonyArra Apr 20 '22 at 03:03

3 Answers3

1

try this

str = str.replace("get", "")
    .replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2")
    .replaceAll("([a-z])([A-Z])", "$1_$2")
Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
0

Use a boolean first-time switch to only put an underscore after the second upper case letter.

Here are some test results.

getTradeID
Trade_ID

Here's the complete runnable code.

public class RemoveGet {

    public static void main(String args[]) {
        args = new String[1];
        args[0] = "getTradeID";
        
        for (String a : args) {
            System.out.println(a);
            String b = a.replace("get", "");
            boolean firstTimeSwitch = true;
//          System.out.println(b);
            StringBuffer sb = new StringBuffer();
            sb.append(b.charAt(0));
            
            for (int i = 1; i < b.length(); i++) {
                if (firstTimeSwitch && Character.isUpperCase(b.charAt(i))) {
                    sb.append("_");
                    sb.append(b.charAt(i));
                    firstTimeSwitch = false;
                } else {
                    sb.append(b.charAt(i));
                }
            }
            
            System.out.println(sb.toString());
        }
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • This works absolutely correct! Thanks. But One more question is if give something like getSeparateUNderScore it is printing Separate_UNderScore but it should print Separate_UNder_Score – Sai Chand Apr 20 '22 at 08:34
  • @Sai Chand: You didn't provide that specification in your original question. You need to define exactly what upper case characters need an underscore. – Gilbert Le Blanc Apr 20 '22 at 10:14
0

Instead of writing all logic in the main function, write some functions to do small tasks and call them in the main function. This makes the code readable and easy to debug. This could be the possible solution code:

public class RemoveGet {

 public static String addUnderScoreAppropriately(String input) {
     String result = "";
     String underScore = "_";
     for(int i=0; i<input.length();i++) {
         if((Character.isUpperCase(input.charAt(i))) && (i != 0)) {
             result = result + underScore + input.charAt(i);
         }else{
             result = result + input.charAt(i);
         }
     }
     result = result.replace("_I_D","_ID");
     return result;
 }

 public static void main(String args[]) {
    for (String a : args) {
        System.out.println(addUnderScoreAppropriately(a.replace("get","")));
    }
 }

}
Deepeshkumar
  • 395
  • 3
  • 13