Java实例大全
方法1:通过 substring()字符串截取方法,从新拼接返回截取后的字符串
@Test
public void RemoveString(){
String str = "this is java";
System.out.println(removeCharAt(str,0));
}
public static String removeCharAt(String s, int pos) {
if(pos<0){
return "-1";
}else if(s.length()-1<pos){
return s;
}
return s.substring(0, pos) + s.substring(pos + 1);
}
结果:
his is java
方法2:通过StringBuilder的delete()删除方法
@Test
public void RemoveString1(){
StringBuilder strb = new StringBuilder();
strb.append("this is java");
System.out.println(strb);
strb.delete(0, 5);
System.out.println(strb);
}
结果:
this is java
is java