public class Test { public static void main(String[] args) { String s = "Hello world I love you"; s=s.replaceAll(" ", "");; System.out.println(s); } }
不好意思看错了 public class Test { public static void main(String[] args) { String s = "Hello world I love you"; s=s.replaceFirst(" ", "");; System.out.println(s); } }
public class Test{ public static void main(String[] args) { String s = "Hello world I love you"; System.out.println(del(5,s)); } public static String del(int index,String str){ StringBuilder sb = new StringBuilder(str); sb.deleteCharAt(index); return sb.toString(); }}
NO 算法.
public static void main(String[] args) {
String s = "Hello world I love you";
s=s.replaceAll(" ", "");;
System.out.println(s);
}
}
public class Test {
public static void main(String[] args) {
String s = "Hello world I love you";
s=s.replaceFirst(" ", "");;
System.out.println(s);
}
}
public class Test{ public static void main(String[] args) {
String s = "Hello world I love you";
System.out.println(del(5,s));
}
public static String del(int index,String str){
StringBuilder sb = new StringBuilder(str);
sb.deleteCharAt(index);
return sb.toString();
}}
System.out.println(s);
System.out.println(s.replaceAll("\\s(\\s*)", "$1")); 好像你还有另外一个帖子吧,我回复了,为何还不结贴?
String s = "Hello world I love you";
char[] chs = s.toCharArray();
boolean findSpace = false;
int offset = 0;
for(int i = 0; i < chs.length; i++) {
if(chs[i] == ' ') {
if(findSpace) {
continue;
}
findSpace = true;
} else {
findSpace = false;
}
chs[offset++] = chs[i];
}
System.out.println(new String(chs, 0, offset));
}
}