如何只显示字符串的第一个字母? [英] How do I show only the first letters of a string?
本文介绍了如何只显示字符串的第一个字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我提示用户输入他们的名字和姓氏,但我无法弄清楚如何只显示他们的第一个和最后一个首字母
I'm prompting the user to enter their first and last name but I can't figure out how to show just their first and last initials
import javax.swing.*;
//import java.awt.*;
public class Lab2Program2 {
public static void main (String [] args) {
String firstName;
String lastName;
firstName = JOptionPane.showInputDialog(null, "What is your first name?");
lastName = JOptionPane.showInputDialog(null, "What is your last name?");
JOptionPane.showMessageDialog(null, " Your initials are " +
firstName + " " + lastName);
}
}
推荐答案
你可以使用 chatAt()
方法。
JOptionPane.showMessageDialog(null,
"Your initials are " + firstName.charAt(0) + " " + lastName.charAt(0));
这篇关于如何只显示字符串的第一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文