2014-04-18 7 views
0

먼저 다음 코드에서 문자를 바꿉니 까? 나는 그것을 어떻게하는지 모른다. (초보자입니다.) 고맙습니다. (. 나는 이미 이전 라인에 사용 된 바와 같이, 내 modifiedValue = temp.substring(0, 6); 라인이 잘못 알고)첫 번째 및 마지막 Char?

public void switched() { 
    String temp; 
    String modifiedValue; 

    temp = inputField.getText(); 
    modifiedValue = temp.substring(0, 6); 
    outputArea.append("With first char last and last char first:\n"); 
    outputArea.append("\t" + modifiedValue + "\n"); 
    outputArea.append("\n"); 

} // end of switched() 

답변

3

문자열의 첫 번째와 마지막 문자를 교체하려면 다음을 수행하십시오

int n = temp.length(); 
temp = temp.charAt(n-1) + temp.substring(1, n-1) + temp.charAt(0); 
0

이 작동 할 수

int n = temp.length(); 
temp = temp.substring(n-1) + temp.substring(1,n-1)+temp.substring(0,1); 
0

유 시도 할 수 있습니다 :

StringBuilder sb = new StringBuilder("foo bar"); 
System.out.println(sb); // foo bar 
sb.append(sb.charAt(0)); 
sb.setCharAt(0, sb.charAt(sb.length() - 2)); 
sb.deleteCharAt(sb.length() - 2); 
System.out.println(sb); // roo baf 
관련 문제