如何从字符串中删除空格?仅使用indexOf()和substring()

时间:2014-12-07 03:49:09

标签: java

这是我到目前为止所写的内容。它检查是否存在空间。如果是,则应将其删除并将删除的空格字符串分配给noSpace。但我不知道如何仅使用indexOf和substring删除空格。有人可以帮忙吗?

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;         
}      

4 个答案:

答案 0 :(得分:4)

如果空格不存在,

string.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);
}

答案 1 :(得分:2)

你只需要反复调用indexOf()直到找不到空格,在每个空格后用substring()重建字符串(通过连接空格前后的部分)。

String noSpace = s;
int idx;
while (-1 != (idx = noSpace.indexOf(" "))) {
    noSpace = noSpace.substring(0,idx) + noSpace.substring(idx+1);
}

答案 2 :(得分:2)

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()));
    }

  }
  

结果:
在remmoveSpace之前:
在remmoveSpace之后:
  在remmoveSpace之前:StartWithEmpty
之后   remmoveSpace:StartWithEmpty
在remmoveSpace之前:有几个   由字母分隔的空格   remmoveSpace:Thereareafewspacesseparatedbyletters
之前   remmoveSpace:Are2SpacesConsessConsecutively After   remmoveSpace:ThereAre2SpacesConsessConsecutively

答案 3 :(得分: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)
  }
}
}