Java 如何计算字符串中所有数字的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21937091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate sum of all numbers in a string
提问by user3337429
How do I calculate the sum of all the numbers in a string? In the example below, the expected result would be 4+8+9+6+3+5
. My attempt is below. Also could I calculate the sum of only those numbers which are divisible by 2?
如何计算字符串中所有数字的总和?在下面的例子中,预期的结果是4+8+9+6+3+5
。我的尝试如下。我也可以只计算那些可以被 2 整除的数字的总和吗?
int sum=0;
String s = "jklmn489pjro635ops";
for(int i=0; i<s.length(); i++) {
char temp = s.charAt(i);
if (Character.isDigit(temp)) {
int b = Integer.parseInt(String.valueOf(temp));
sum=sum+b;
}
}
System.out.println(sum);
回答by VinayVeluri
Some thing like this..
像这样的东西..
import org.apache.commons.lang.StringUtils;
public static void main(String[] args) {
String a = "jklmn489pjro635ops";
int length = a.length();
char c;
int count = 0;
for(int i=0;i<length;i++) {
c = a.charAt(i);
if(StringUtils.isNumeric(String.valueOf(c))) {
count = count + Integer.parseInt(String.valueOf(c));
}
}
System.out.println(count);
}
回答by user2795559
int sum = 0;
for(int i = 0; i < str.length()-1;){
try{
sum += Integer.parseInt(str.substring(i, ++i));
}catch(Exception e){}
}
回答by user2795559
String input = "abc12xx3";
Pattern pattern = Pattern.compile("[\d]");
Matcher matcher = pattern.matcher(input);
int sum = 0;
while(matcher.find()) {
sum += Integer.parseInt(matcher.group());
}
return sum;
回答by renke
Parsing chars back to String
and then to Integer
is too expensive, since you already have a char. You should try doing this:
将字符解析回 toString
和 toInteger
太昂贵了,因为您已经有了一个字符。你应该尝试这样做:
String a = "jklmn489pjro635ops";
int sum = 0;
int evenSum = 0;
for (char c : a.replaceAll("\D", "").toCharArray()) {
int digit = c - '0';
sum += digit;
if (digit % 2 == 0) {
evenSum += digit;
}
}
System.out.println(sum);
System.out.println(evenSum);
回答by Jess
Here's a full mainline example you can run. This should answer your question and all the follow on questions in the comments.
这是您可以运行的完整主线示例。这应该回答您的问题以及评论中的所有问题。
import java.util.regex.*;
public class StringSum {
public static void main(String[] args) {
int sum = 0;
Pattern pattern = Pattern.compile("([2468])"); // Who do we appreciate!
Matcher matcher = pattern.matcher(args[0]);
while (matcher.find()) {
sum += Integer.parseInt(matcher.group(1));
}
System.out.println(sum);
}
}
Some example executions:
一些示例执行:
$ javac StringSum.java && java StringSum jklmn489pjro635ops
18
$ javac StringSum.java && java StringSum a1b2c3d4
6
回答by Jess
int banana = 0;
String meow = "74387493";
for (int i = 0; i < meow.length(); i++)
{
String cows = ""+meow.charAt(i);
int b = Integer.parseInt(cows);
banana = banana + b;
}
System.out.println(banana);
The above code will add all the numbers in a string (sorry for the silly names).
上面的代码将添加一个字符串中的所有数字(对不起,愚蠢的名字)。
回答by vbh
String a = "jklmn489pjro635ops";
int sum = 0;
for(int i = 0; i < a.length(); i++) {
if(Character.isDigit(a.charAt(i))) {
sum = sum + Integer.parseInt(a.charAt(i) + "");
}
}
System.out.println(sum);
回答by Dark Army
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String input = sc.next();
int sum = 0;
char[] c = input.toCharArray();
for(int i = 0;i<=c.length-1;i++)
{
if(Character.isDigit(c[i]))
{
Character c1 = c[i];
String s1 = c1.toString();
int i1 = Integer.parseInt(s1);
sum = sum+i1;
}
}
System.out.println(sum);
回答by Kundan Shinde
This could be a simple and efficient way:
这可能是一种简单而有效的方法:
static int sumFromString(String s){
int sum = 0;
for(int i = 0; i < s.length() ; i++){
if( Character.isDigit(s.charAt(i)) ){
sum = sum + Character.getNumericValue(s.charAt(i));
}
}
return sum;
}
Another way was to convert char to String and then to int, which is not so efficient:
另一种方法是将 char 转换为 String,然后转换为 int,这样效率不高:
sum = sum + Integer.parseInt(s.charAt(i) + "");
回答by Asim Maqsood
I think this would be the simplest and easiest way
我认为这将是最简单和最简单的方法
static int sumOfString(String str){
int sum = 0;
for(int i=0; i<str.length(); i++){
if(Character.isDigit(str.charAt(i))){
sum+=str.charAt(i)-'0';
}
}
return sum;
}