[英]Convert numbers in substring to words in java
我是java的新手。 我想将子字符串中的数字转换为字符串中具有特定位置的英语单词。
例如,字符串ABC12345DEFG
和substring(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);
1)你不应该替换所有子串(3,8),因为你真的只想一次替换一个字符。
2)你的线路每次都会改变newStr。 最后一次是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);
尝试这样的事情:
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" )
返回此:
ABConetwothreefourfiveDEF
您可以改进的几件事情是:
更改
String words = {"zero", "one", "two", "three", "four", "five"};
至
String[] words = new String[]{"zero", "one", "two", "three", "four", "five"};
它是一个字符串数组,这是表示它的一种正确方法。
在你的循环中,改变
if (i==j){ newStr = str.replace(str.substring(3,8), words[j]; }
至
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"};
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);
产量
ABConetwothreefourfiveDEFG
所有其他人给他代码但没有指出他的代码的错误。 这对他来说并不好,因为他正在学习新语言。 我指出内联推荐如下:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.