1. 程式人生 > >將字串【"Hello world" 】 倒序結果為【"world Hello " 】

將字串【"Hello world" 】 倒序結果為【"world Hello " 】

public void exchange(){
    String arr= "Hello world";
    // 每個空格處進行分割
String s[] = arr.split(" ");
    // 通過迴圈輸出陣列內容
for(int i=0; i<s.length; i++){
        System.out.printf("s內容為"+ s[i] +"\n");
    }

    // 實現倒序轉換
for(int j=0; j<s.length; j++){
        String temp = s[j];
        s[j] = s[s.length
-1];
        s[s.length-1] = temp;
    }

    // 使用Array類輸出陣列內容
System.out.printf("s倒序後的內容為"+ Arrays.toString(s) +"\n");
    // 陣列轉換成字串
String ss = org.apache.commons.lang.StringUtils.join(s," ");
    System.out.printf("SS 內容為" + ss);