如何在 Java 中打印字符串中每个单词的第一个字符?
String 类可用于表示字符串,Java 程序中的所有字符串文字都作为 String 类的实例实现。字符串是常量,其值一旦创建就不能更改(不可变)。
我们可以使用以下程序打印字符串中每个单词的第一个字符。
示例
public class FirstCharacterPrintTest {
public static void main(String[] args) {
String str = "Welcome To Tutorials Point";
char c[] = str.toCharArray();
System.out.println("The first character of each word: ");
for (int i=0; i < c.length; i++) {
// Logic to implement first character of each word in a string
if(c[i] != ' ' && (i == 0 || c[i-1] == ' ')) {
System.out.println(c[i]);
}
}
}
}
输出
The first character of each word: W T T P
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP