解析和替换Java字符串中的双精度
我正在编写用于转换信号的java代码。如果给定字符串(输入)为: 其翻译(输出)应为:解析和替换Java字符串中的双精度,java,regex,parsing,double,substring,Java,Regex,Parsing,Double,Substring,我正在编写用于转换信号的java代码。如果给定字符串(输入)为: 其翻译(输出)应为: C*12387:1000a0d14assc7*1865:100d142a 算法: 在字符串中,如果一个星号(*)跟在一个包含小数的数字后面(在该字符串中有两个,第一个是“*12.387”,第二个是*18.65”),则该数字将被转换为分数,如上例所示,12.387转换为12387:1000,18.65转换为1865:100 如果十进制数是孤立的,我可以用以下代码将其转换为分数: double d =
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("\0", "0");
String replacement = "*" + num + denom + ":" + divisor;
m.appendReplacement(result, replacement);
}
m.appendTail(result);
System.out.println(result.toString());
我正在写一个使用正则表达式的解决方案,但后来尝试不使用正则表达式。这里的解决方案是通用的(适用于任何编程语言)。当然,看看它是否比基于正则表达式的解决方案更快会很有趣。无论如何,我怀疑基于正则表达式的解决方案可能会更快。请也看看这个解决方案(虽然不是完美的:))
请添加提示,重复字符串需要StringUtils外部库请参阅。没有外部库:)替换了外部库此代码的输出非常令人震惊:C*12387:1C*12.387a0d14assc7*18.65d142aC*12.387a0d14assc7*18.65d142aC*12.387a0d14assc7*18.65d142aa0d14assc7*1865:1C*12.387a0d14assc7*18.65d142aC*12.387a0d14assc7*18.65D142A删除外部库依赖项时出现复制粘贴错误。现在已修复…您的此代码非常有竞争力。它很有教育意义,这在所有伟大的书籍中都是缺失的,它解决了我的许多其他类似问题。它给了我洞察力和创新能力。
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("\0", "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("\0","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