如何从字符串中删除所有字符,直到一定范围内的字符为止?

huangapple go评论100阅读模式
英文:

How to remove all character from a string until a range of accepted characters?

问题

例如,我有一个字符串"0.01"。如何获得"1"

我尝试过:

  1. String a = "0.01"
  2. a = a.replaceAll(".", "");
  3. a = a.replaceAll("0", "");

但是这不起作用,因为字符串可能是"0.0105",在这种情况下,我想保留"105"

我还尝试过:

  1. String a = "0.01"
  2. b = a.substring(s.indexOf("0")+3);

这也不起作用,因为字符串可能是"0.1",我想保留"1"

简而言之,我想要删除所有的0.,直到它以非零数开头。该字符串实际上是从Double转换而来的。我不能简单地对Double执行*100,因为它可以是0.1

英文:

For example, I have a string "0.01". How can I get "1"?

I had tried

  1. String a = "0.01"
  2. a = a.replaceAll(".", "");
  3. a = a.replaceAll("0", "");

but this won't work as the string can be "0.0105" and in this case, I want to keep "105"

I also tried

  1. String a = "0.01"
  2. b = a.substring(s.indexOf("0")+3);

this also won't work as the string can be "0.1" which I want to keep "1"

In short, I want to remove all 0 or . until it starts with non-0. The string is actually converted from Double. I can't simply *100 with the Double as it can be 0.1

答案1

得分: 2

  1. String a = "0.01";
  2. String[] b = a.split("\\.");
  3. a = b[0] + b[1];
  4. int c = Integer.parseInt(a);
  5. // You will get 1 as an integer. When you want to add something you can add and then return it to a string like:
  6. c = c + 3;
  7. String newa = String.valueOf(c);
  8. System.out.println(newa);
英文:
  1. String a = "0.01";
  2. String[] b = a.split("\\.");
  3. a = b[0]+b[1];
  4. int c = Integer.parseInt(a);

You will get 1 as integer. When you want to add something you can add and then return to string like:

  1. c = c+3;
  2. String newa = String.valueOf(c);
  3. System.out.println(newa);

答案2

得分: 2

只需执行:

  1. System.out.println(Integer.valueOf(testCase.replace(".", "")));

原文出自YCF_L

英文:

Just do:

  1. System.out.println(Integer.valueOf(testCase.replace(".", "")));

Credit to YCF_L

答案3

得分: 0

使用正则表达式 [1-9][0-9]*,它表示第一个数字为 1-9,然后后续的数字(可选的)为 0-9。从 Lesson: Regular Expressions 中了解更多关于正则表达式的内容。

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. public class Main {
  4. public static void main(String[] args) {
  5. String str = "0.0105";
  6. Pattern pattern = Pattern.compile("[1-9][0-9]*");
  7. Matcher matcher = pattern.matcher(str);
  8. if (matcher.find()) {
  9. str = matcher.group();
  10. }
  11. System.out.println(str);
  12. }
  13. }

输出:

  1. 105
英文:

Use the regex, [1-9][0-9]* which means the first digit as 1-9 and then subsequent digits (optional) as 0-9. Learn more about regex from Lesson: Regular Expressions.

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. public class Main {
  4. public static void main(String[] args) {
  5. String str = "0.0105";
  6. Pattern pattern = Pattern.compile("[1-9][0-9]*");
  7. Matcher matcher = pattern.matcher(str);
  8. if (matcher.find()) {
  9. str = matcher.group();
  10. }
  11. System.out.println(str);
  12. }
  13. }

Output:

  1. 105

huangapple
  • 本文由 发表于 2020年10月11日 00:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64295864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定