试图将另一个char替换为map

时间:2015-03-05 11:26:35

标签: java

我试图用OU替换W只有当他前面的字符在字符串的位置0并且它是一个consonnant并且只有字符串中的第一个W,即TWITWIC - > TOUITWIC。

这是我的代码(卫生棉条是我在测试中输入代码的输入):

public class Phonkw {
    static Map<String, String> consonnantReplace = new HashMap<String, String>();
    static {
         consonnantReplace.put("BW", "BOU");
         consonnantReplace.put("CW", "COU");
         consonnantReplace.put("DW", "DOU");
         consonnantReplace.put("FW", "FOU");
         consonnantReplace.put("GW","GOU");
         consonnantReplace.put("HW","HOU");
         consonnantReplace.put("JW", "JOU");
         consonnantReplace.put("KW", "KOU");
         consonnantReplace.put("LW", "LOU");
         consonnantReplace.put("MW", "MOU");
         consonnantReplace.put("NW", "NOU");
         consonnantReplace.put("PW", "POU");
         consonnantReplace.put("QW", "QOU");
         consonnantReplace.put("RW", "ROU");
         consonnantReplace.put("SW", "SOU");
         consonnantReplace.put("TW", "TOU");
         consonnantReplace.put("VW","VOU");
         consonnantReplace.put("WW", "WOU");
         consonnantReplace.put("XW","XOU");
         consonnantReplace.put("ZW", "ZOU");            
    }

    public static String phonkw1(final String tampon){
        if (tampon==null){
            return "";
        }
        if(consonnantReplace.containsKey(tampon)){
            return consonnantReplace.get(tampon);
        }
        return tampon;
    }

}

我认为我需要在(0,1)处对子棉条进行子串,但是我不能在地图中获取子串的值。

编辑:解决。

3 个答案:

答案 0 :(得分:1)

如果您想坚持使用Map解决方案,则确实需要使用substring()。对于查找,您希望使用substring(0, 2),因为这将是String tampon的前两个字符。但是,请注意,如果是单字母单词,这将throw StringIndexOutOfBoundsException,因此需要检查长度的警卫。

这是相应修改的代码:

import java.util.*;

public class Phonkw {
    static Map<String, String> consonnantReplace = new HashMap<String, String>();
    static {
         consonnantReplace.put("BW", "BOU");
         consonnantReplace.put("CW", "COU");
         consonnantReplace.put("DW", "DOU");
         consonnantReplace.put("FW", "FOU");
         consonnantReplace.put("GW","GOU");
         consonnantReplace.put("HW","HOU");
         consonnantReplace.put("JW", "JOU");
         consonnantReplace.put("KW", "KOU");
         consonnantReplace.put("LW", "LOU");
         consonnantReplace.put("MW", "MOU");
         consonnantReplace.put("NW", "NOU");
         consonnantReplace.put("PW", "POU");
         consonnantReplace.put("QW", "QOU");
         consonnantReplace.put("RW", "ROU");
         consonnantReplace.put("SW", "SOU");
         consonnantReplace.put("TW", "TOU");
         consonnantReplace.put("VW","VOU");
         consonnantReplace.put("WW", "WOU");
         consonnantReplace.put("XW","XOU");
         consonnantReplace.put("ZW", "ZOU");            
    }

    public static String phonkw1(final String tampon){
        if (tampon == null){
            return "";
        }
        if (tampon.length() >= 2) {
            final String key = tampon.substring(0, 2);
            if (consonnantReplace.containsKey(key)) {
                return consonnantReplace.get(key) + tampon.substring(2);
            }
        }
        return tampon;
    }

    public static void main(final String... args) {
        for (final String arg : args)
            System.out.println(phonkw1(arg));
    }
}

您实际上可以使用循环创建Map<String, String>。如果您不打算在运行时修改Map,则还可以使用Collections.unmodifiableMap()对其进行包装,以防止意外修改。

你可以解决拼写错误,它是consonant,而不是consonnant

代码看起来像这样:

import java.util.*;

public class Phonkw {
    private static final Map<String, String> consonantReplace = createConsonantReplaceMap();

    private static Map<String, String> createConsonantReplaceMap() {
        final Map<String, String> map = new HashMap<>();
        final String consonants = "BCDFGHJKLMNPQRSTVWXZ";
        for (final char consonant : consonants.toCharArray())
            map.put(consonant + "W", consonant + "OU");
        return Collections.unmodifiableMap(map);
    }

    public static String phonkw1(final String tampon) {
        if (tampon == null) return "";
        if (tampon.length() < 2) return tampon;
        final String key = tampon.substring(0, 2);
        if (consonantReplace.containsKey(key))
            return consonantReplace.get(key) + tampon.substring(2);
        return tampon;
    }

    public static void main(final String... args) {
        for (final String arg : args)
            System.out.println(phonkw1(arg));
    }
}

如果所有条目都是统一替换,则您实际上不需要Map。在这种情况下,您可以直接检查,如下所示:

public class Phonkw {
    private static final String CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ";
    public static boolean isConsonant(final char c) {
        return CONSONANTS.indexOf(c) != -1;
    }
    public static String phonkw1(final String tampon) {
        if (tampon == null) return "";
        if (tampon.length() < 2) return tampon;
        if (tampon.charAt(1) == 'W' && isConsonant(tampon.charAt(0)))
            return tampon.charAt(0) + "OU" + tampon.substring(2);
        return tampon;
    }

    public static void main(final String... args) {
        for (final String arg : args)
            System.out.println(phonkw1(arg));
    }
}

使用正则表达式,代码可以更简单:

public class Phonkw {
    public static String phonkw1(final String tampon) {
        return tampon == null ? "" : tampon.replaceAll("^([BCDFGHJKLMNPQRSTVWXZ])W", "$1OU");
    }

    public static void main(final String... args) {
        for (final String arg : args)
            System.out.println(phonkw1(arg));
    }
}

答案 1 :(得分:0)

您应该使用regular expression来解决此问题,请在评论中查看正确使用的正则表达式

如果您想使用非正则表达式解决方案,可以考虑以下代码。您确实可以使用String.substring,或使用String.charAt作为单个字符

public class Phonkw {
    public static String phonkw1(final String tampon){
        if (tampon==null){
            return "";
        }
        if (tampon.length() > 1 && isConsonant(tampon.charAt(0)) {//the first character is a consonant
            if (tampon.charAt(1) == 'W') {//the 2nd character is a W
                return tampon.charAt(0) + "OU" + tampon.substring(2); // not an optimal solution..
            }
        }
        return tampon;
    }

    public static boolean isConsonant(char c) {
        return "BCDFGHJKLMNPQRSTVWXZ".indexOf(c) != -1;
    }
}

答案 2 :(得分:0)

下面的代码应该替换'W'之前的任何内容,这是辅音,但前提是该辅音位于字符串中的位置0。

public class Phonkw {
    static Map<String, String> consonnantReplace = new HashMap<String, String>();
    static {
        consonnantReplace.put("BW", "BOU");
        consonnantReplace.put("CW", "COU");
        consonnantReplace.put("DW", "DOU");
        consonnantReplace.put("FW", "FOU");
        consonnantReplace.put("GW", "GOU");
        consonnantReplace.put("HW", "HOU");
        consonnantReplace.put("JW", "JOU");
        consonnantReplace.put("KW", "KOU");
        consonnantReplace.put("LW", "LOU");
        consonnantReplace.put("MW", "MOU");
        consonnantReplace.put("NW", "NOU");
        consonnantReplace.put("PW", "POU");
        consonnantReplace.put("QW", "QOU");
        consonnantReplace.put("RW", "ROU");
        consonnantReplace.put("SW", "SOU");
        consonnantReplace.put("TW", "TOU");
        consonnantReplace.put("VW", "VOU");
        consonnantReplace.put("WW", "WOU");
        consonnantReplace.put("XW", "XOU");
        consonnantReplace.put("ZW", "ZOU");
    }

public static String phonkw1(String tampon) {
    if (tampon == null) {
        return "";
    }

    Iterator<String> it = consonnantReplace.keySet().iterator();

    while (it.hasNext()) {
        String s = it.next();
        if (tampon.indexOf(s) == 0) {
            tampon = tampon.replace(s, consonnantReplace.get(s));
        }

    }

    return tampon;
}

}

正则表达式可能是另一种解决方案吗?