Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何仅为整数部分修剪字符串_Java_Regex - Fatal编程技术网

Java 如何仅为整数部分修剪字符串

Java 如何仅为整数部分修剪字符串,java,regex,Java,Regex,我有一个字符串,它可以有像“45”,“45.00”,“45.0”,“45.000”,“45.23”等值。对于所有这些,我想保存“45”,如果它的小数部分都是0,否则它将是“45.23” 我怎么做?我应该为此使用正则表达式吗?您可以将字符串拆分为小数点,然后取第一个元素: java.util.Arrays.toString(testString.split("\\.") 是的,你可以使用正则表达式,试试这个 s = s.replaceAll("(.?)(\\.0+)?", "$1");

我有一个字符串,它可以有像
“45”
“45.00”
“45.0”
“45.000”
“45.23”
等值。对于所有这些,我想保存
“45”
,如果它的小数部分都是
0
,否则它将是
“45.23”


我怎么做?我应该为此使用正则表达式吗?

您可以将字符串拆分为小数点,然后取第一个元素:

java.util.Arrays.toString(testString.split("\\.")

是的,你可以使用正则表达式,试试这个

    s = s.replaceAll("(.?)(\\.0+)?", "$1");

使用StringTokenizer和。作为定界者

如果第二个令牌为零,则忽略第二个令牌,否则考虑第二个令牌

示例代码

StringTokenizer st = StringTokenizer(s,".");
int count = 0;
while(st.hasTokens)
{
 if (count==1)
 {
   // Read the token and compare it to zero and modify the original string s and your further processing
 }
 else{
    st.nextToken();
    count++;
 }
}
使用以下命令:

s.replaceAll("\\.0+", "")

嗯,在搜索了一下之后,我想我找到了你要找的东西。不需要正则表达式来做这件事。Java中有一个专门用于十进制格式化的类,名为,并建议您在本文中了解如何用Java格式化数字。以下是一个例子:

    public class NumberFormat {

    public static void main(String[] args){
        DecimalFormat myFormatter = new DecimalFormat("###.##");
        System.out.println(myFormatter.format(45));
        System.out.println(myFormatter.format(45.000));
        System.out.println(myFormatter.format(45.23));
        System.out.println(myFormatter.format(45.258));

    }
}
生成输出:

  45
  45
  45,23
  45,26

我假设字符串由
分隔

如果您想要双打列表,可以使用:

List<Double> lst = new ArrayList<Double>();
for(String s : yourString.split(",\\s?")) {
    try {
        Double double = Double.valueOf(s);
        lst.add(double);
    } catch(NumberFormatException nfe) {
        nfe.printStackTrace();
    }
}
List<Number> lst = new ArrayList<Number>();
for(String s : yourString.split(",\\s?")) {
    try {
        Double double = Double.valueOf(s);
        if(double == (int)double) {
            lst.add(double);
        } else {
            lst.add((Integer)double.intValue());
        }
    } catch(NumberFormatException nfe) {
        nfe.printStackTrace();
    }
}
List lst=new ArrayList();
对于(字符串s:yourString.split(“,\\s?”)){
试一试{
Double Double=Double.valueOf(s);
第一次添加(双倍);
}捕获(NumberFormatException nfe){
nfe.printStackTrace();
}
}
如果您想要数字列表,可以使用:

List<Double> lst = new ArrayList<Double>();
for(String s : yourString.split(",\\s?")) {
    try {
        Double double = Double.valueOf(s);
        lst.add(double);
    } catch(NumberFormatException nfe) {
        nfe.printStackTrace();
    }
}
List<Number> lst = new ArrayList<Number>();
for(String s : yourString.split(",\\s?")) {
    try {
        Double double = Double.valueOf(s);
        if(double == (int)double) {
            lst.add(double);
        } else {
            lst.add((Integer)double.intValue());
        }
    } catch(NumberFormatException nfe) {
        nfe.printStackTrace();
    }
}
List lst=new ArrayList();
对于(字符串s:yourString.split(“,\\s?”)){
试一试{
Double Double=Double.valueOf(s);
如果(双精度==(整数)双精度){
第一次添加(双倍);
}否则{
lst.add((整数)double.intValue());
}
}捕获(NumberFormatException nfe){
nfe.printStackTrace();
}
}

你尝试过什么吗?要么使用正则表达式,要么解析浮点数,然后对它们进行取整。安德里:你能详细说明一下吗?或者给我一个正则表达式你看了DecimalFormat类了吗?@Dimitri:不知道,它将如何解决我的目的点
需要像
testString.split(\\”)一样转义
split()
在正则表达式上工作。感谢您的澄清。它只能执行45.34到45.34和45.0到45吗?我终于明白您的意思了,修正了,现在是45.34->45.34;45.0 -> 45