如何在 Java 中将 String 作为一个 int 流来处理
假设我们有以下字符串
String str = "YuBM787Nm";
现在,若要以 IntStream 的形式显示字符串,请按以下方式使用 filter() 和 map()
int res = str.chars() .filter(Character::isDigit) .map(ch → Character.valueOf((char) ch)).sum();
以下是一个显示字符串为 IntStream 的示例
示例
public class Demo {
public static void main(String[] args) {
String str = "YuBM787Nm";
int res = str.chars() .filter(Character::isDigit) .map(ch -> Character.valueOf((char) ch)).sum();
System.out.println("String as IntStream = "+res);
}
}输出
String as IntStream = 166
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP