Friday, May 9, 2014

Java Replace Camel Case String With Another String In That Place

MainClass.java

package com.pkm;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainClass {
    public static String mainString = "PritomKumarMondal";
    
    public static void main(String[] args) {
        System.out.println("Method: getString()");
        System.out.println("\t" + getString());
        System.out.println("Method: getString2()");
        System.out.println("\t" + getString2());
        System.out.println("Method: getString3()");
        System.out.println("\t" + getString3());
        System.out.println("Method: getString4()");
        System.out.println("\t" + getString4());
        System.out.println("Method: getString5()");
        System.out.println("\t" + getString5());
    }
    
    public static String getString() {
        System.out.println("\t" + MainClass.mainString);
        return MainClass.mainString.replaceAll("[^a-zA-Z0-9]+", "").replaceAll(
                String.format("%s|%s|%s",
                "(?<=[A-Z])(?=[a-z])",
                "(?<=[^A-Z])(?=[A-Z])",
                "(?<=[A-Za-z])(?=[^A-Za-z])"
                ),
            "."
        ).toLowerCase();
    }
    
    public static String getString2() {
        System.out.println("\t" + MainClass.mainString);
        String string = MainClass.mainString.replaceAll(
                String.format("%s",
                "(?=[A-Z])(?=[A-Z])"
                ),
            "."
        ).toLowerCase();
        string = string.replaceAll("[^a-zA-Z0-9.]+", "");
        return string.startsWith(".") ? string.substring(1) : string;
    }
    
    public static String getString3() {
        System.out.println("\t" + MainClass.mainString);
        return MainClass.mainString.replaceAll(
                String.format("%s",
                "(?<=[^A-Z])(?=[A-Z])"
                ),
            "."
        ).toLowerCase().replaceAll("[^a-zA-Z0-9.]+", "");
    }
    
    public static String getString4() {
        System.out.println("\t" + MainClass.mainString);
        String str1 = MainClass.mainString;
        str1 = str1.replaceAll("[A-Z]+", ".$0").toLowerCase().replaceAll("[^a-zA-Z0-9.]+", "");
        str1 = str1.startsWith(".") ? str1.substring(1) : str1;
        return str1;
    }
    
    public static String getString5() {
        System.out.println("\t" + MainClass.mainString);
        String str1 = MainClass.mainString;
        Pattern pattern = Pattern.compile("[A-Z]+");
        Matcher matcher = pattern.matcher(str1);
        StringBuffer output = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(output, "." + matcher.group().toLowerCase());
        }
        matcher.appendTail(output);
        str1 = output.toString().replaceAll("[^a-zA-Z0-9.]+", "");
        return str1.startsWith(".") ? str1.substring(1) : str1;
    }
}

Output using: 'sUIKAm009Pritom KumarMondalUUID'


Method: getString()
 sUIKAm009Pritom KumarMondalUUID
 s.uika.m.009.p.ritom.k.umar.m.ondal.uuid
Method: getString2()
 sUIKAm009Pritom KumarMondalUUID
 s.u.i.k.am009.pritom.kumar.mondal.u.u.i.d
Method: getString3()
 sUIKAm009Pritom KumarMondalUUID
 s.uikam009.pritom.kumar.mondal.uuid
Method: getString4()
 sUIKAm009Pritom KumarMondalUUID
 s.uikam009.pritom.kumar.mondal.uuid
Method: getString5()
 sUIKAm009Pritom KumarMondalUUID
 s.uikam009.pritom.kumar.mondal.uuid

Output using: 'PritomKumarMondal'


Method: getString()
 PritomKumarMondal
 p.ritom.k.umar.m.ondal
Method: getString2()
 PritomKumarMondal
 pritom.kumar.mondal
Method: getString3()
 PritomKumarMondal
 pritom.kumar.mondal
Method: getString4()
 PritomKumarMondal
 pritom.kumar.mondal
Method: getString5()
 PritomKumarMondal
 pritom.kumar.mondal

Output using: 'Pritom KUMAR MonDAL'


Method: getString()
 Pritom KUMAR MonDAL
 p.ritom.kumarm.on.dal
Method: getString2()
 Pritom KUMAR MonDAL
 pritom.k.u.m.a.r.mon.d.a.l
Method: getString3()
 Pritom KUMAR MonDAL
 pritom.kumar.mon.dal
Method: getString4()
 Pritom KUMAR MonDAL
 pritom.kumar.mon.dal
Method: getString5()
 Pritom KUMAR MonDAL
 pritom.kumar.mon.dal

No comments:

Post a Comment