日期:2014-05-20 浏览次数:20857 次
public class UpperCase { public static void main(String[] args) { System.out.println(getTheUpperCase("good", 0)); } static String getTheUpperCase(String src, int pos) { if(pos>=src.length()) throw new RuntimeException("指定的位置不合法!"); StringBuilder sb = new StringBuilder(src); sb.setCharAt(pos, Character.toUpperCase(src.charAt(pos))); return sb.toString(); } }
------解决方案--------------------
字符位置从1开始表示,返回null表示传入的参数有误
public static String upperCaseString(String str,int pos) { if(pos < 1 || pos > str.length() || null == str | "".equals(str)) return null; char[] strChars = str.toCharArray(); strChars[pos-1] = Character.toUpperCase(strChars[pos-1]); return String.valueOf(strChars); }
------解决方案--------------------
public static String upperCaseString(String str,int pos)
{
if(pos < 1 || pos > str.length() || null == str | "".equals(str)) return null;
char[] strChars = str.toCharArray();
strChars[pos-1] = Character.toUpperCase(strChars[pos-1]);
return String.valueOf(strChars);
}