Java:有没有其他有效的方法来做到这一点

时间:2016-10-10 10:43:32

标签: java

输入: - >我有不同的组名,如:

  

(gis06352.b.com,corp.abcd.b.com,corp.def.nam.b.com,abc)

必需输出:

  

(why_gis06352_not,why_abcd_not,why_def_not,why_abc_not)

CODE:

package a;

public class Demo {

public static void main(String[] args) {
    System.out.println("Group name : "
            + getgroupName("abc.logon.ds.b.com"));
}

public static String getTestGrpName(String groupName) {

    int idxOf;
    int idxOfLstDot;
    if (groupName != null && !"".equals(groupName)) {
        if (groupName.contains(".nam.corp.b.com")) {
            idxOf = groupName.indexOf(".nam.corp.b.com");
            groupName = groupName.substring(0, idxOf);
            idxOfLstDot = groupName.lastIndexOf(".") + 1;
            groupName = "Why"
                    + groupName.substring(idxOfLstDot) + "Not";

        } else if (groupName.contains(".corporate.b.com")) {
            idxOf = groupName.indexOf(".corporate.b.com");
            groupName = groupName.substring(0, idxOf);
            idxOfLstDot = groupName.lastIndexOf(".") + 1;
            groupName = "Why"
                    + groupName.substring(idxOfLstDot) + "Not";

        } else if (groupName.contains(".gis.corp.b.com")) {
            idxOf = groupName.indexOf(".gis.corp.b.com");
            groupName = groupName.substring(0, idxOf);
            idxOfLstDot = groupName.lastIndexOf(".") + 1;
            groupName = "Why"
                    + groupName.substring(idxOfLstDot) + "Not";

        } else if (groupName.contains(".logon.ds.b.com")) {
            idxOf = groupName.indexOf(".logon.ds.b.com");
            groupName = groupName.substring(0, idxOf);
            idxOfLstDot = groupName.lastIndexOf(".") + 1;
            groupName = "Why"
                    + groupName.substring(idxOfLstDot) + "Not";

        } else if (groupName.contains("copr1.b.com")) {
            idxOf = groupName.indexOf("copr1.b.com");
            groupName = groupName.substring(0, idxOf);
            idxOfLstDot = groupName.lastIndexOf(".") + 1;
            groupName = "Why"
                    + groupName.substring(idxOfLstDot) + "Not";

        } else if (groupName.contains(".nam.b.com")) {
            idxOf = groupName.indexOf(".nam.b.com");
            groupName = groupName.substring(0, idxOf);
            idxOfLstDot = groupName.lastIndexOf(".") + 1;
            groupName = "Why"
                    + groupName.substring(idxOfLstDot) + "Not";

        } else if (groupName.contains(".b.com")) {
            idxOf = groupName.indexOf(".b.com");
            groupName = groupName.substring(0, idxOf);
            idxOfLstDot = groupName.lastIndexOf(".") + 1;
            groupName = "Why"
                    + groupName.substring(idxOfLstDot) + "Not";
        }

        else {
            groupName = "Why" + groupName + "Not";
        }
    }
    return groupName;
}
}

输出:

  

why_abc_not

2 个答案:

答案 0 :(得分:0)

创建临时变量并根据该临时变量组合公共线。

温度=真;

if (groupName != null && !"".equals(groupName)) {
        if (groupName.contains(".nam.corp.b.com")) {
            idxOf = groupName.indexOf(".nam.corp.b.com");

        } else if (groupName.contains(".corporate.b.com")) {
            idxOf = groupName.indexOf(".corporate.b.com");


        } else if (groupName.contains(".gis.corp.b.com")) {
            idxOf = groupName.indexOf(".gis.corp.b.com");

        } else if (groupName.contains(".logon.ds.b.com")) {
            idxOf = groupName.indexOf(".logon.ds.b.com");


        } else if (groupName.contains("copr1.b.com")) {
            idxOf = groupName.indexOf("copr1.b.com");

        } else if (groupName.contains(".nam.b.com")) {
            idxOf = groupName.indexOf(".nam.b.com");

        } else if (groupName.contains(".b.com")) {
            idxOf = groupName.indexOf(".b.com");

        }
        else {
            groupName = "Why" + groupName + "Not";
            temp=false;
        }
        if(temp=true){
            groupName = groupName.substring(0, idxOf);
            idxOfLstDot = groupName.lastIndexOf(".") + 1;
            groupName = "Why"+ groupName.substring(idxOfLstDot) + "Not";
        }
    }
    return groupName;

}

答案 1 :(得分:0)

我的口味将是以下。修改为你的。 : - )

private static final String[] knownGroupNames = { ".nam.corp.b.com", ".corporate.b.com", ".gis.corp.b.com",
        ".logon.ds.b.com", "copr1.b.com", ".nam.b.com", ".b.com" };

public static String getTestGrpName(String groupName) {
    if (groupName == null || groupName.isEmpty()) {
        return groupName;
    }
    for (String knownGroupName : knownGroupNames) {
        if (groupName.contains(knownGroupName)) {
            int idxOf = groupName.indexOf(knownGroupName);
            groupName = groupName.substring(0, idxOf);
            int idxOfLstDot = groupName.lastIndexOf(".") + 1;
            return "Why" + groupName.substring(idxOfLstDot) + "Not";
        }
    }
    return "Why" + groupName + "Not";
}