Java得到字符串某个元素下标并修改元素值 电玩女神 2024-04-03 10:11 24阅读 0赞 ### Java得到字符串某个元素下标 ### 第一种 /** * @param url,s,i * @return int * @author shuws */ public static int getCharacterPosition(String url,String s,int i){ //这里是获取"/"符号的位置 lastindexof从字符串末尾开始检索,检索到子字符 Matcher slashMatcher = Pattern.compile(s).matcher(url); int mIdx = 0; while(slashMatcher.find()) { mIdx++; //当"/"符号第i次出现的位置 if(mIdx == i){ break; } } return slashMatcher.start(); } 第二种 // 0 0 8 16/2 10-12 ? 2022-2023 String cronExpression = "0 0 8 16/2 10-12 ? 2022-2023"; String[] splitArray = cronExpression.split(" "); splitArray[3] = dayFrom+"/"+updateBy; splitArray[4] = monthFrom+"-"+monthTo; splitArray[6] = yearFrom+"-"+yearTo; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < splitArray.length; i++) { String s = splitArray[i]; if (i == splitArray.length-1 ){ stringBuilder.append(s); }else { stringBuilder.append(s+" "); } }
还没有评论,来说两句吧...