2015-11-03 104 views
2

我是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); 

    } 
} 

我如何在第一個和最後一個字母之間添加單詞?

+0

字符串lettersBetween = word.substring(1,stringLength - 1) ? – RuntimeException

+1

你似乎知道如何接受一部分字符串。您只需在第一個字母和最後一個字母之前參加並將其添加到第一個和最後一個字符的高版本。 – Pshemo

回答

2

代碼看起來很好,只是做一些改變

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 
0

你可以嘗試以下

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

這應該給你的這個詞的「中間」,而第一個和最後一個字母

+0

@Brandon Laidig,它只會放棄倒數第3個字符 –

+0

@ ankur-singhal是的,謝謝。我理解substring是包含性的,即子字符串(1,3)將抓取索引爲1,2和3的字符。感謝修正 –

1

嘗試是這樣的:

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

在S特林。 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); 

然後,只需建立自己的新詞(第一個字符,再加上中間部分,加上最後一個字符)並轉換相關部分加時賽上的情況下使用String 。 upperCase()方法

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

你可以簡單地做

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

+1

只是簡單的?這是我認爲可以編寫代碼的最簡單的方式:P –

0

由於大家都把一個建議,將工作;我正在調整一個更有效和安全的建議。

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

我這樣做對我的頭頂,我希望這不會有任何錯別字

0

/**代碼添加字符串句的第一個到最後一個,第二個到第二個,第三個到第三個最後一個和 作者:deependra singh patel */

import java.util.Scanner;

公共類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辛格巴特爾thegun singhraur

expected output :18singhraurdeependra 11thegunsingh 5patel  

*/

相關問題