在java中将子字符串中的数字转换为单词

在java中将子字符串中的数字转换为单词,java,string,Java,String,我是java新手。我想将子字符串中的数字转换为字符串中具有特定位置的英语单词 例如,字符串ABC12345DEFG和子字符串(3,8)应输出以下内容 ABConetwothreefourfiveDEF 我尝试使用以下代码,但它只返回ABCfiveDEFG。你能帮我解决这个问题吗 String str = "ABC12345DEFG"; String newStr = ""; String words = {"zero", "one", "two", "three", "four", "five

我是java新手。我想将子字符串中的数字转换为字符串中具有特定位置的英语单词

例如,字符串
ABC12345DEFG
子字符串(3,8)
应输出以下内容

ABConetwothreefourfiveDEF
我尝试使用以下代码,但它只返回
ABCfiveDEFG
。你能帮我解决这个问题吗

String str = "ABC12345DEFG";
String newStr = "";
String words = {"zero", "one", "two", "three", "four", "five"};
for (char c:str.toCharArray()){
    int i = (int)(c-'0');
    for (int j=0; j<words.length; j++){
        if (i==j){
            newStr = str.replace(str.substring(3,8), words[j];
        }
    }
}
System.out.println(newStr);
String str=“ABC12345DEFG”;
字符串newStr=“”;
字符串={“零”、“一”、“二”、“三”、“四”、“五”};
for(字符c:str.toCharArray()){
int i=(int)(c-'0');

对于(int j=0;j代码的逻辑错误

试一种更简单的方法

    String str = "ABC12345DEFG";
    String newStr = "";
    String[] words = {"zero", "one", "two", "three", "four", "five"};
    for (char c:str.toCharArray()){
        int i = (int)(c-'0');
        if (i >= 1 && i <= 5) // or (words.length - 1)
        {
            newStr += words[i];
        }
        else {
            newStr += c;
        }
    }
    System.out.println(newStr);
1) 您不应该替换所有的子字符串(3,8),因为您实际上只想一次替换一个字符

2) 你的台词每次点击都会改变新闻。最后一次是5次,这就是为什么你只有“ABCfiveDEFG”

3) 在数组中循环找到一个等于i的索引…很奇怪吗?如果j==i,那么就使用i,确保它在范围内

String str = "ABC12345DEFG";
String newStr = "";
String words = {"zero", "one", "two", "three", "four", "five"};
for (char c:str.toCharArray()){
    int i = (int)(c-'0');
    if (i >= 0 && i < words.length)
        newStr += words[i];
    else
        newStr += c;
}
System.out.println(newStr);
String str=“ABC12345DEFG”;
字符串newStr=“”;
字符串={“零”、“一”、“二”、“三”、“四”、“五”};
for(字符c:str.toCharArray()){
int i=(int)(c-'0');
如果(i>=0&&i
尝试以下方法:

public static String exchange( final String txt, final String... numbers ) {
    String result = txt;
    for ( int i = 0; i < numbers.length; ++i ) {
        result = result.replace( Integer.toString( i ), numbers[i] );
    }
    return result;
}
exchange( "ABC12345DEF", "zero", "one", "two", "three", "four", "five" )
返回以下内容:

abconetweetweefourvedef


您可以改进的几点是:

  • 改变

    String words = {"zero", "one", "two", "three", "four", "five"};
    

  • 这是一个字符串数组,这是表示它的一种正确方法

  • 在你的循环中,改变

    if (i==j){
        newStr = str.replace(str.substring(3,8), words[j];
    }
    


  • 相反,您试图用数组中的一个字符串替换整个数字字符子字符串。

    所有其他字符串都给了他代码,但没有指出代码的错误。这对他不好,因为他正在学习新语言。我指出如下:

    String str = "ABC12345DEFG";
    String newStr = "";
    String words = {"zero", "one", "two", "three", "four", "five"}; // Im not recommend to declare this defination because this data as a library. So you should map them as: 1->one, 2->two,... IF you just define as array, if the index is wrong => the data will be wrong
    for (char c:str.toCharArray()){
        int i = (int)(c-'0'); // Should not declare variable inside the loop
        for (int j=0; j<words.length; j++){ // There are 2 loop => O(n2) => bad performance
            if (i==j){
                newStr = str.replace(str.substring(3,8), words[j]; // Wrong replace here, that why the final result is wrong
            }
        }
    }
    System.out.println(newStr);
    
    String str=“ABC12345DEFG”;
    字符串newStr=“”;
    String words={“zero”、“one”、“two”、“three”、“four”、“five”};//我不建议声明此定义,因为此数据是一个库。因此您应该将它们映射为:1->one,2->two,…如果您仅定义为数组,如果索引错误=>数据将错误
    for(字符c:str.toCharArray()){
    int i=(int)(c-'0');//不应在循环内声明变量
    对于(int j=0;j O(n2)=>性能差
    如果(i==j){
    newStr=str.replace(str.substring(3,8),单词[j];//此处替换错误,这就是最终结果错误的原因
    }
    }
    }
    System.out.println(newStr);
    
    我的代码不是最好的,但我相信它有良好的性能和明确的定义

    public static void main(String[] args){
        String str = "ABC12345DEFG";
        Map<Integer, String> numberInWord = new ConcurrentHashMap<Integer, String>(5) {{
        put(0,"zero"); put(1,"one"); put(2,"two"); put(3,"three"); put(4,"four"); put(5,"five");
        }};
        for(Map.Entry<Integer, String> entry : numberInWord.entrySet()){
            str = str.replace(entry.getKey().toString(), entry.getValue());
        }
        System.out.println(str);
    }
    
    publicstaticvoidmain(字符串[]args){
    字符串str=“ABC12345DEFG”;
    Map numberInWord=新的ConcurrentHashMap(5){{
    放(0,“零”);放(1,“一”);放(2,“二”);放(3,“三”);放(4,“四”);放(5,“五”);
    }};
    for(Map.Entry:numberInWord.entrySet()){
    str=str.replace(entry.getKey().toString(),entry.getValue());
    }
    系统输出打印项次(str);
    }
    
    作为附带主题,最好只依赖ascii码并映射到字符串数组中的单词,这样就可以用O(n)而不是O(n2)完成。因此,根据您的代码,您正在用
    单词数组中的每个元素替换
    12345
    ,当您想用适当的单词替换一个数字时-因此您的逻辑从一开始就处于关闭状态。可以使用
    StringBuilder
    来存储结果,从
    str
    中提取每个字符,检查是否它是否是一个数字,并将结果附加到
    StringBuilder
    if (i == j) {
        newStr = str.replace(Character.toString(c), words[j]); // replace the numeric character with equivalent string
        str = newStr; // update the base string
     }
    
    String str = "ABC12345DEFG";
    String newStr = "";
    String words = {"zero", "one", "two", "three", "four", "five"}; // Im not recommend to declare this defination because this data as a library. So you should map them as: 1->one, 2->two,... IF you just define as array, if the index is wrong => the data will be wrong
    for (char c:str.toCharArray()){
        int i = (int)(c-'0'); // Should not declare variable inside the loop
        for (int j=0; j<words.length; j++){ // There are 2 loop => O(n2) => bad performance
            if (i==j){
                newStr = str.replace(str.substring(3,8), words[j]; // Wrong replace here, that why the final result is wrong
            }
        }
    }
    System.out.println(newStr);
    
    public static void main(String[] args){
        String str = "ABC12345DEFG";
        Map<Integer, String> numberInWord = new ConcurrentHashMap<Integer, String>(5) {{
        put(0,"zero"); put(1,"one"); put(2,"two"); put(3,"three"); put(4,"four"); put(5,"five");
        }};
        for(Map.Entry<Integer, String> entry : numberInWord.entrySet()){
            str = str.replace(entry.getKey().toString(), entry.getValue());
        }
        System.out.println(str);
    }