如何查找一系列数字,并将这些数字乘以2(使用JAVA)?

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

How to find a sequence of digits and multiply those sequence of digits with number 2 in JAVA?

问题

在JAVA中如何查找数字序列并仅将这些数字序列与数字2相乘?

假设我有一个字符串"My age is 19"。如何得到一个字符串"My age is 38"作为输出?

此外,我如何在稍后的字符串中反转已乘以的数字?对于上面的示例输入,第二个输出应该是"My age is 83"。

我尝试使用java.util.regex.Pattern。它有效。但是,是否还有其他方法可以做到这一点?可能通过将字符串转换为数组?我尝试过。但我卡在了将字符串中的字符序列相乘。相反,我能够将每个数字乘以2。但我想要将2位数字19乘以2。

英文:

How to find a sequence of digits and multiply only those sequence of digits with number 2 in JAVA?

Let's say I have a string "My age is 19". How do I get a string "My age is 38" as the output?

Also, how do I reverse that multiplied number in the string later on. For above example input, the second output should be "My age is 83"

I tried using java.util.regex.Pattern. It works. However, is there any other way we can do that. Probably through converting the string into an array? I tried. But I am struck at multiplying sequence of characters in the string. Instead I am able to multiple each digit. But I want the sequence of digits to be multiplied by 2. For instance, in above example, I am able to multiply each digit 1 and 9 by 2. But I want 19 as a 2 digit number to be multiplied by 2.

答案1

得分: 1

我假设您是在说将整个字符串“My age is 20”乘以2。如果是这样的话,没有办法得到您期望的输出。

您可以这样做,循环遍历整个字符串,找到一个看起来是数字形式的部分,并检查该部分。有几种方法可以检查字符串是否为数字,在以下链接中讨论了其中一些方法。

https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java

在那之后,您可以在(解析后的)数字字符串上进行乘法运算,然后将它们添加回字符串中。

英文:

I'm assuming you are saying multiplying the whole string "My age is 20" by 2. If so, there is no way to achieve your desired output.

What you can do is, loop over the whole string, find a section which appear to be in numeric form and check for that section. There are several ways to check if a string is digit and some of them are discussed in the following link.

https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java

After that, you can perform multiplication on a (parsed) digit-string then add them back to the string.

答案2

得分: 1

你可以使用正则表达式并进行类似这样的操作

// 使用带有表示数字的分组的正则表达式模式
Pattern pattern = Pattern.compile("My age is (\\d+)");
// 从字符串中获取数字
Matcher matcher = new Matcher(pattern, "My age is 20");
matcher.find();
int age = Integer.parseInt(matcher.group(1));
// 乘法运算
int result = age * 2;
英文:

You can use regular expression and do something like this

    //set regexp pattern with group representing number
    Pattern pattern = Pattern.compile("My age is (\\d+)");
    //get number from string
    Matcher matcher = new Matcher(pattern, "My age is 20");
    matcher.find();
    int age = Integer.parseInt(matcher.group(1));
    //multiply
    int result = age * 2;

答案3

得分: 1

试试这个。

    Pattern pat = Pattern.compile("\\d+");
    String s = "My age is 19";
    String doubled = pat.matcher(s).replaceAll(m -> "" + Integer.valueOf(m.group()) * 2);
    System.out.println(doubled);
    String reversed = pat.matcher(doubled).replaceAll(m -> new StringBuilder(m.group()).reverse().toString());
    System.out.println(reversed);

输出:

My age is 38
My age is 83
英文:

Try this.

Pattern pat = Pattern.compile("\\d+");
String s = "My age is 19";
String doubled = pat.matcher(s).replaceAll(m -> "" + Integer.valueOf(m.group()) * 2);
System.out.println(doubled);
String reversed = pat.matcher(doubled).replaceAll(m -> new StringBuilder(m.group()).reverse().toString());
System.out.println(reversed);

output

My age is 38
My age is 83

huangapple
  • 本文由 发表于 2020年9月6日 13:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63761148.html
匿名

发表评论

匿名网友

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

确定