问题:
有一个由大小写组成的字符串,现在需要对它进行修改,将其中的所有小写字母排在大写字母的前面(大写或小写字母之间不要求保持原来次序)。
解决办法:
设置2个指针i和j,i最初指向字符串的第一个位置,j指向字符串的最后一个位置;
i向后遍历直到遇到第一个大写字符,j向前遍历直到遇到第一个小写字母,交换i和j位置上的字符;
直到i=j+1,结束;注意交换前需要判断i是否小于j,否则最后的两个字符不正确。
| 
       1 
      2 
      3 
      4 
      5 
      6 
      7 
      8 
      9 
      10 
      11 
      12 
      13 
      14 
      15 
      16 
      17 
      18 
      19 
      20 
      21 
      22 
      23 
      24 
      25 
      26  | 
    
      public String lowerBeforeUpper(String s){        int 
len =s.length();        int 
i=0;        int 
j=len-1;        while(i<j){            while(i<len&&s.charAt(i)>=‘a‘&&s.charAt(i)<=‘z‘){   //xiaoxie                i++;            }            while(j>=0&&s.charAt(j)>=‘A‘&&s.charAt(j)<=‘Z‘){                j--;            }            if(i<j){                s = switchIJ(s,i,j);                }        }        return 
s;    }         private 
String switchIJ(String s, int 
i, int j) {        // TODO Auto-generated method stub        StringBuffer a = new 
StringBuffer(s);        char 
temp= a.charAt(i);        a.setCharAt(i, a.charAt(j));        a.setCharAt(j, temp);        return 
a.toString();    } | 
原文地址:http://www.cnblogs.com/lxq0309/p/3719048.html