简体   繁体   中英

Java - Replacing many strings at once

So I'm trying to write a program that takes a String (str) and converts it to NATO Phonetic Alphabet (newSentence).

This is what I tried:

newSentence = str.toLowerCase().replace("a", "Alpha ")
                               .replace("b", "Bravo ")
                               .replace("c", "Charlie ")
                               .replace("d", "Delta ")
                               .replace("e", "Echo ")
                               .replace("f", "Foxtrot ")
                               .replace("g", "Golf ")
                               .replace("h", "Hotel ")
                               .replace("i", "India ")
                               .replace("j", "Juliet ")
                               .replace("k", "Kilo ")
                               .replace("l", "Lima ")
                               .replace("m", "Mike ")
                               .replace("n", "November ")
                               .replace("o", "Oscar ")
                               .replace("p", "Papa ")
                               .replace("q", "Quebec ")
                               .replace("r", "Romeo ")
                               .replace("s", "Sierra ")
                               .replace("t", "Tango ")
                               .replace("u", "Uniform ")
                               .replace("v", "Victor ")
                               .replace("w", "Whiskey ")
                               .replace("x", "X-Ray ")
                               .replace("y", "Yankee ")
                               .replace("z", "Zulu ");

However, this obviously doesn't work since after it replaces every "a" with "Alpha," it'll take every "l" "p" and "h" and reconvert those and so on. Is there some way to convert all of the letters at once so that this doesn't happen, and make it much more efficient?

Create a new string iteratively:

StringBuilder outSB = new StringBuilder();
for(int i=0; i < str.length(); i++){
    if(str.charAt(i) == 'a' || str.charAt(i) == 'A') outSB.append("Alpha ");
    if(str.charAt(i) == 'b' || str.charAt(i) == 'B') outSB.append("Bravo ");
    // and so on
}
String output = outSB.toString();
private static Map<Character, String> lookup = new HashMap<>();
public static void main(String[] args) {
    String input = "abc";
    StringBuilder sb = new StringBuilder();

    for(char c : input.toCharArray()){
        sb.append(lookup.get(c));
        sb.append(" ");
    }

    System.out.println(sb.toString());

}

static {
    lookup.put('a', "Alpha");
    lookup.put('b', "Bravo");
    lookup.put('c', "Charlie");
}

Output:

Alpha Bravo Charlie

Create a lookup table and construct a new string based on each character in your original string.

StringBuilder sb = new StringBuilder();
for (char c : str.toLowerCase().toCharArray()) {
    switch (c) {
        case 'a':  
            sb.append("Alpha ");
            break;
        case 'b':  
            sb.append("Bravo ");
            break;
        // and so on until Zulu...
        default:
            // optional: just pass through any character not in NATO alphabet
            sb.append(c);
            break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM