解析和替换 Java 字符串中的双精度数
Parsing and replacing doubles in Java String
我正在编写 java 转换信号的代码。如果给定的字符串 (INPUT) 是:
C*12.387a0d14assc7*18.65d142a
它的翻译(输出)应该是:
C*12387:1000a0d14assc7*1865:100d142a
算法:
字符串中凡是星号(*)后面有小数的数字(本字符串中有两个,第一个是'*12.387',第二个是*18.65'),这个数字要变成小数如上例所示,12.387 转换为 12387:1000,18.65 转换为 1865:100
如果小数是孤立的,我可以用下面的代码将它转换成分数:
double d = 12.387;
String str = Double.toString(d);
String[] fraction = str.split("\.");
int denominator = (int)Math.pow(10, fraction[1].length());
int numerator = Integer.parseInt(fraction[0] + "" + fraction[1]);
System.out.println(numerator + ":" + denominator);
但我不知道如何将十进制数字的子串与其包含的字符串分开。作为 java 和编程的新手,我需要帮助。感谢期待。
使用正则表达式和捕获组是实现解析的好方法:
String s = "C*12.387a0d14assc7*18.65d142a";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile("\*(\d+)\.(\d+)").matcher(s);
while (m.find()) {
String num = m.group(1);
String denom = m.group(2);
String divisor = "1" + new String(new char[denom.length()]).replace("[=10=]", "0");
String replacement = "*" + num + denom + ":" + divisor;
m.appendReplacement(result, replacement);
}
m.appendTail(result);
System.out.println(result.toString());
我正在编写一个使用正则表达式的解决方案,但后来尝试不使用它们。这里的解决方案是通用的(适用于任何编程语言)。当然,看看它是否比基于正则表达式的解决方案更快会很有趣。无论如何,我怀疑基于正则表达式的解决方案可能会更快。也请参阅此解决方案(虽然它并不完美 :))。
import java.util.*;
class DoubleConvert
{
public static void main (String[] args)
{
StringBuilder buffer = new StringBuilder("C*12.387a0d14assc7*18.65d142a");
int j, m, k;
int i = 0;
while (i < buffer.length())
{
if (buffer.charAt(i) == '*')
{
m = -1; k = -1;
j = i; //remember where * found
while ( i + 1 < buffer.length() )
{
i++;
if (Character.isDigit(buffer.charAt(i)))
{
continue;
}
else if (buffer.charAt(i) == '.')
{
m = i; // remember where . found
while (i + 1 < buffer.length())
{
i++;
if (Character.isDigit(buffer.charAt(i)))
{
continue;
}
else
{
k = i; //remember the last position
break;
}
}
}
else //let's see what we got
{
if (m > 0 && j > 0 && m - j > 0 && k - m > 0) //there must exist strings
{
System.out.println("Found " + buffer.substring(j, m)
+ " second part " + buffer.substring(m, k));
buffer.replace(j+1, k,
buffer.substring(j+1, m) +
buffer.substring(m+1, k) +
":1" +
new String(new char[k-1-m]).replace("[=10=]","0"));
}
break;
}
}
}
else
{
i++;
}
}
System.out.println("Result " + buffer);
}
}
输出
Found *12 second part .387
Found *18 second part .65
Result C*12387:1000a0d14assc7*1865:100d142a
我正在编写 java 转换信号的代码。如果给定的字符串 (INPUT) 是:
C*12.387a0d14assc7*18.65d142a
它的翻译(输出)应该是:
C*12387:1000a0d14assc7*1865:100d142a
算法:
字符串中凡是星号(*)后面有小数的数字(本字符串中有两个,第一个是'*12.387',第二个是*18.65'),这个数字要变成小数如上例所示,12.387 转换为 12387:1000,18.65 转换为 1865:100
如果小数是孤立的,我可以用下面的代码将它转换成分数:
double d = 12.387;
String str = Double.toString(d);
String[] fraction = str.split("\.");
int denominator = (int)Math.pow(10, fraction[1].length());
int numerator = Integer.parseInt(fraction[0] + "" + fraction[1]);
System.out.println(numerator + ":" + denominator);
但我不知道如何将十进制数字的子串与其包含的字符串分开。作为 java 和编程的新手,我需要帮助。感谢期待。
使用正则表达式和捕获组是实现解析的好方法:
String s = "C*12.387a0d14assc7*18.65d142a";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile("\*(\d+)\.(\d+)").matcher(s);
while (m.find()) {
String num = m.group(1);
String denom = m.group(2);
String divisor = "1" + new String(new char[denom.length()]).replace("[=10=]", "0");
String replacement = "*" + num + denom + ":" + divisor;
m.appendReplacement(result, replacement);
}
m.appendTail(result);
System.out.println(result.toString());
我正在编写一个使用正则表达式的解决方案,但后来尝试不使用它们。这里的解决方案是通用的(适用于任何编程语言)。当然,看看它是否比基于正则表达式的解决方案更快会很有趣。无论如何,我怀疑基于正则表达式的解决方案可能会更快。也请参阅此解决方案(虽然它并不完美 :))。
import java.util.*;
class DoubleConvert
{
public static void main (String[] args)
{
StringBuilder buffer = new StringBuilder("C*12.387a0d14assc7*18.65d142a");
int j, m, k;
int i = 0;
while (i < buffer.length())
{
if (buffer.charAt(i) == '*')
{
m = -1; k = -1;
j = i; //remember where * found
while ( i + 1 < buffer.length() )
{
i++;
if (Character.isDigit(buffer.charAt(i)))
{
continue;
}
else if (buffer.charAt(i) == '.')
{
m = i; // remember where . found
while (i + 1 < buffer.length())
{
i++;
if (Character.isDigit(buffer.charAt(i)))
{
continue;
}
else
{
k = i; //remember the last position
break;
}
}
}
else //let's see what we got
{
if (m > 0 && j > 0 && m - j > 0 && k - m > 0) //there must exist strings
{
System.out.println("Found " + buffer.substring(j, m)
+ " second part " + buffer.substring(m, k));
buffer.replace(j+1, k,
buffer.substring(j+1, m) +
buffer.substring(m+1, k) +
":1" +
new String(new char[k-1-m]).replace("[=10=]","0"));
}
break;
}
}
}
else
{
i++;
}
}
System.out.println("Result " + buffer);
}
}
输出
Found *12 second part .387
Found *18 second part .65
Result C*12387:1000a0d14assc7*1865:100d142a