Java 如何从字符串中删除空格?仅使用indexOf()和substring()
这是我到目前为止写的。它检查空间是否存在。如果是,它应该删除它并将删除的空格字符串分配给noSpace。但我不知道如何仅使用indexOf和substring删除空格。有人能帮忙吗Java 如何从字符串中删除空格?仅使用indexOf()和substring(),java,Java,这是我到目前为止写的。它检查空间是否存在。如果是,它应该删除它并将删除的空格字符串分配给noSpace。但我不知道如何仅使用indexOf和substring删除空格。有人能帮忙吗 public static String removeSpace(String s) { String noSpace = ""; if(s.indexOf(" ") == -1) { noSpace = s; } else { for(int a = 1;
public static String removeSpace(String s)
{
String noSpace = "";
if(s.indexOf(" ") == -1)
{
noSpace = s;
}
else
{
for(int a = 1; a <= s.length(); a++)
{
if()
noSpace = s.substring(0,a);
}
}
return noSpace;
}
公共静态字符串removeSpace(字符串s)
{
字符串noSpace=“”;
如果(s.indexOf(“”)=-1)
{
noSpace=s;
}
其他的
{
for(int a=1;astring.indexOf(“”)
将在不存在空格的情况下返回-1。否则,它将返回空格第一个实例的索引。因此,您只需检查它是否返回-1以外的内容:
int index = s.indexOf(" ");
if(index != -1) {
}
然后,通过将子字符串上移到空格,然后从空格后的索引中剪切出空格:
noSpace = s.substring(0,index) + s.substring(index + 1);
就这样
你会得到这样的结果:
String s = "space testing";
while(s.indexOf(" ") != -1) {
s = s.substring(0, s.indexOf(" ")) + s.substring(s.indexOf(" ") + 1);
}
您可以剪切并附加字符串以删除空白
String str = "abc asd";
while(true){
int whiteSpaceIndex = str.indexOf(" ");
if(whiteSpaceIndex != -1) {
// if the space was the last character of String ?
// Yes --> then don't care for part after first clip
// No --> then append the part after the space character
int nextStartIndex = str.length() - whiteSpaceIndex > 1 ? whiteSpaceIndex + 1 : whiteSpaceIndex;
if(nextStartIndex != whiteSpaceIndex) {
str = str.substring(0, whiteSpaceIndex) + str.substring(nextStartIndex);
}else {
str = str.substring(0, whiteSpaceIndex)
}
}
}
您只需重复调用indexOf()
,直到找不到更多的空格,然后在每个空格后使用substring()
重新生成字符串(通过连接空格前后的部分)
结果:
在remmoveSpace之前:
在remmoveSpace之后:
删除前移动空间:删除后开始
remmoveSpace:StartWithEmpty
在remmoveSpace之前:有几个
后面用字母分隔的空格
remmoveSpace:之前有几个由字母分隔的空间
remmoveSpace:之后连续出现2个空格
remmoveSpace:连续存在2个空间连接
你不能只使用这两个函数。你至少还需要,concat
。他可以,但最好使用其他函数@HotLicks@HotLicks看看我的答案,我想你可能错了…@HotLicks这绝对有可能。无论如何都不推荐,但可能。我的老师允许我使用其他功能s、 因为我们必须遵循collegeboard的指导原则,所以这是AP Com Scitrim()
将节省不必要的迭代,但前导或尾随空格在这方面不是问题case@outlyer实际上,在这种情况下,你是对的。我们不需要:)很好的调用!所以我测试了代码,但是因为s包含空格,while循环不是无限的吗?这就是我的编译器says@PTheCoolGuy否,s
会随着循环的每次迭代而改变,删除一个空格,直到某个点没有空格,因此无法满足循环条件anymore@PTheCoolGuy我刚刚运行了这个代码你自己,它工作得很好。它会移除空间,直到不再满足条件。
String noSpace = s;
int idx;
while (-1 != (idx = noSpace.indexOf(" "))) {
noSpace = noSpace.substring(0,idx) + noSpace.substring(idx+1);
}
public static void main(String[] args) {
String testString = " ";
System.out.println("Before remmoveSpace:" + testString);
System.out.println("After remmoveSpace:" + removeSpace(testString));
testString = " StartWithEmpty";
System.out.println("Before remmoveSpace:" + testString);
System.out.println("After remmoveSpace:" + removeSpace(testString));
testString = "There are a few spaces separated by letters ";
System.out.println("Before remmoveSpace:" + testString);
System.out.println("After remmoveSpace:" + removeSpace(testString));
testString = "There Are2SpacesConsessConsecutively";
System.out.println("Before remmoveSpace:" + testString);
System.out.println("After remmoveSpace:" + removeSpace(testString));
}
public static String removeSpace(String s) {
int firstIndexOfSpace = s.indexOf(" ");
if (firstIndexOfSpace == -1) {
return s;
} else {
return s.substring(0, firstIndexOfSpace)
+ removeSpace(s.substring(firstIndexOfSpace + 1, s.length()));
}
}