用 Java 将字符串转换成 IntStream 的程序
原文:https://www . geesforgeks . org/program-to-convert-string-to-int stream-in-Java/
给定一个字符串,任务是将这个字符串转换成一个包含字符的 ASCII 值作为元素的输入流。
例:
Input: String = Geeks
Output: 71, 101, 101, 107, 115
Input: String = GeeksForGeeks
Output: 71, 101, 101, 107, 115, 70, 111, 114, 71, 101, 101, 107, 115
算法:
- Gets the string to be converted.
- Use the chars () method to convert it to IntStream.
- Printed IntStream.
以下是上述方法的实现:
// Java program to convert
// String to IntStream
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
// Get the String to be converted
String string = "Geeks";
// Print the String
System.out.println("String: " + string);
// Convert String to IntStream using chars() method
IntStream intStream = string.chars();
// Print the elements of the IntStream
intStream.forEach(System.out::println);
}
}
输出:
String: Geeks
71
101
101
107
115
版权属于:月萌API www.moonapi.com,转载请注明出处