How do I show only the first letters of a string?

前端 未结 3 1870
悲&欢浪女
悲&欢浪女 2021-01-29 17:00

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          


        
相关标签:
3条回答
  • 2021-01-29 17:22

    You can use chatAt() method.

    JOptionPane.showMessageDialog(null, 
                "Your initials are " + firstName.charAt(0) + " " + lastName.charAt(0));
    
    0 讨论(0)
  • 2021-01-29 17:22

    firstName.charAt(0) returns the char value at the 0 index. That is the first letter of your firstName String.

    0 讨论(0)
  • 2021-01-29 17:36

    If you want to show a message box like you were doing before, this should work:

    JOptionPane.showMessageDialog(null, 
        "Your initials are " + firstName.substring(0, 1) + " " + lastName.substring(0, 1));
    
    0 讨论(0)
提交回复
热议问题