使用String方法在java中大写第一个和最后一个字母

我是java的初学者,我们被要求将这个词的第一个和最后一个字母大写。

[提示:将字母捕获为字符串(使用子字符串方法),然后使用toUpperCase()方法。]

到目前为止我只有这个,

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 ); } } 

如何在第一个和最后一个字母之间添加单词?

代码看起来很好,只需进行一些更改

 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 a = "java"; String first = a.substring(0, 1).toUpperCase(); String last = a.substring((a.length() - 1), a.length()).toUpperCase(); String middle = a.substring(1, a.length() - 1); System.out.println(first + middle + last); 

你可以干脆做

 String st="singhakash"; System.out.println(st.substring(0,1).toUpperCase()+st.substring(1,st.length()-1)+st.substring(st.length()-1).toUpperCase()); 

产量

 SinghakasH 

DEMO

字符串。 substring()方法会给你一个子串。 所以将这个单词分成几部分,就像你已经部分地做了一样: –

 String startChar = word.substring(0, 1); String endChar = word.substring(word.length()-1); //If you leave out the last argument, it goes all the way to the end of the String String middle = word.substring(1, word.length()-1); 

然后只需构建你的新单词(第一个字符,加上中间部分,再加上最后一个字符),然后使用字符串转换大写的相关部分。 upperCase()方法

  String newWord = startChar.toUpperCase() + middle + endChar.toUpperCase(); 

您可以尝试以下方法

 String letter1 = word.substring (0,1); String lastletter = word.substring ((stringLength-1),(stringLength)); String newWord =letter1.toUpperCase() + word.subString(1, stringLength-1) + lastletter.toUpperCase(); 

这应该给你这个词的“中间”,没有第一个和最后一个字母

因为每个人都提出了可行的建议; 我正在调整一个更有效和更安全的建议。

 String word = keyboard.nextLine(); 

这不保证一个字。 它返回一行,表示换行符’\ n’之前的任何内容。

为了得到这些词,你应该使用:

 String[] words = keyboard.nextLine().split(new char[] { ' ', '\t', '\r' }); 

这将分割字符:空格,制表符和回车符(注意:不是换行符)。

接下来,我们需要使用for-each循环遍历每个单词:

 for(String word : words) { int wordLength = word.length(); // Get length, because we're gonna use it multiple times if(wordLength > 0) // Ignore empty words { // Only get the first character and convert it to uppercase (better than using substring) // Also note that we don't have to check if there's a character at 0, because we already did with wordLength > 0 char firstCharacter = Character.toUpperCase(word.charAt(0)); StringBuilder builder = new StringBuilder(firstCharacter); // We use StringBuilder class because it is efficient in concatenating objects to a string if(wordLength == 2) // We only got 2 characters, so no inbetween { char lastCharacter = Character.toUpperCase(word.charAt(1)); // We know this character is at index 1 builder.append(lastCharacter); // Append last character } else // More than 2 { String inbetweenCharacters = word.substring(1, wordLength - 2); // Get the words in between using substring char lastCharacter = Character.toUpperCase(word.charAt(wordLength - 1)); // Get the last character builder.append(inbetweenCharacters); // Append inbetween characters builder.append(lastCharacter); // Append last character } System.out.println(builder.toString()); // Print complete converted word } } 

我做到了这一点,我希望它没有任何错别字

/ **代码添加字符串句子的第1个到第2个,第2个到第2个,第3个到第3个和作者:deependra singh patel * /

import java.util.Scanner;

public class ReverseStringWordConcate {

  public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println("Enter The Sentence"); String s1 = scn.nextLine(); String []s2 = s1.split(" "); int i1 = 0; for(int i =s2.length-1;i>=0;i--) { if(s2[i]==s2[i1] ){ char [] arr = s2[i].toCharArray(); System.out.print(arr.length+s2[i1]); break; } else { //System.out.print(s2[i]+s2[i1]+" "); char [] arr = s2[i].toCharArray(); char arr1[] = s2[i1].toCharArray(); System.out.print((arr.length+arr1.length)+s2[i]+s2[i1]+" "); } i1++; } } } 

/ *样本输入:deependra singh patel thegun singhraur

  expected output :18singhraurdeependra 11thegunsingh 5patel 

* /