使用String方法在java中大写第一个和最后一个字母 [英] Capitalizing first and last letter in java using String methods
本文介绍了使用String方法在java中大写第一个和最后一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是java的初学者,我们被要求将大写该单词的第一个和最后一个字母。
I'm a beginner in java and we were asked to that will capitalize the first and last letter of the word.
[提示:将字母捕获为字符串(使用子字符串方法),然后使用toUpperCase()方法。]
[Hint: Capture the letter as a string (using the substring method) then use the toUpperCase() method.]
到目前为止我只有这个,
all i have so far is this,
import java.util.Scanner;
public class Excercise6 {
public static void main (String [] args){
Scanner keyboard = new Scanner (System.in);
System.out.print("Please Type a word: ");
String word = keyboard.nextLine();
int stringLength = word.length();
String letter1 = word.substring (0,1);
String lastletter = word.substring ((stringLength-1),(stringLength));
String newWord =letter1.toUpperCase() + lastletter.toUpperCase();
System.out.println("The word with first letter capitalized is: " + newWord );
}
}
如何在两者之间添加单词第一个和最后一个字母??
How do I add the words between the first and the last letter??
推荐答案
代码看起来不错,只需进行一些更改
Code looks fine, just make some changes
String newWord = letter1.toUpperCase()
+ word.substring(1, word.length() - 1)
+ lastletter.toUpperCase();
第一封信 - letter1.toUpperCase()
中间字符串 - word.substring(1,word.length() - 1)
最后一封信 - lastletter.toUpperCase();
输出
Please Type a word: ankur
The word with first letter capitalized is: AnkuR
这篇关于使用String方法在java中大写第一个和最后一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文