替换掉下划线并让紧跟它后面的字母大写时间:2022-07-20 17:00:30替换掉下划线并让紧跟它后面的字母大写 方法一: public class StringUtil { /** * @param args */ public static void main(String[] args) { System.out.println(replaceUnderlineAndfirstToUpper("ni_hao_abc","_","")); } /** * 首字母大写 * * @param srcStr * @return */ public static String firstCharacterToUpper(String srcStr) { return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1); } /** * 替换字符串并让它的下一个字母为大写 * @param srcStr * @param org * @param ob * @return */ public static String replaceUnderlineAndfirstToUpper(String srcStr,String org,String ob) { String newString = ""; int first=0; while(srcStr.indexOf(org)!=-1) { first=srcStr.indexOf(org); if(first!=srcStr.length()) { newString=newString+srcStr.substring(0,first)+ob; srcStr=srcStr.substring(first+org.length(),srcStr.length()); srcStr=firstCharacterToUpper(srcStr); } } newString=newString+srcStr; return newString; } } 方法二 import java.lang.*; public class AAA{ public static void main(String args[]) { //Java替换掉下划线并让紧跟它后面的字母大写 StringBuffer sb = new StringBuffer(); sb.append("asd_fgh_jkl"); int count = sb.indexOf("_"); while(count!=0){ int num = sb.indexOf("_",count); count = num+1; if(num!=-1){ char ss = sb.charAt(count); char ia = (char) (ss - 32); sb.replace(count,count+1,ia+""); System.out.println(sb.toString()); } } String ss = sb.toString().replaceAll("_",""); System.out.println(ss+"--------------"); //JAVA去掉或替换指定子字符或空格等 // String str = "a b c d e f"; // String test = str.replaceAll("//s{1,}", ""); // System.out.println(test); String str = "aaaaaaaaabbbbbbaaaaaaaa"; String test = str.replaceAll("b", "c"); System.out.println(test); } }