public class indexOf |
{ |
public static void main(String[] args) |
{ |
//String.indexOf(start, subStr); |
//index = s.indexOf(index, subStr); |
//int i = s.indexOf("<input"); // 获取匹配到第一个input的位置 |
//System.out.println(i); |
|
String str = "<input type='text'><input" ; |
int i = 0 ; |
while (i>= 0 ) |
{ |
try |
{ |
i = str.indexOf( "<input" , i); //下面不进行++i的话,i就会一直匹配第一个不会往后匹配 |
if ( 0 != ++i) // 如果最后没匹配到就会返回-1,-1++ = 0,循环条件是i>=0,不进行这个if处理就会死循环 |
str = replaceIndex(i- 1 ,str, ">" ); |
else |
break ; |
} |
catch (StringIndexOutOfBoundsException e) |
{ |
e.printStackTrace(); |
} |
} |
System.out.println(str); |
} |
public static String replaceIndex( int index,String str,String res){ // 替换指定位置的字符 |
try { |
str = str.substring( 0 , index) + res + str.substring(index+ 1 ); |
} |
catch (StringIndexOutOfBoundsException e){ |
e.printStackTrace(); |
} |
return str; |
} |
} |
by: 发表于:2017-12-28 14:03:16 顶(0) | 踩(0) 回复
??
回复评论