简体   繁体   中英

How can I remove spaces from a string? only using indexOf() and substring()

This is what Ive written so far. It checks to see whether a space exists. If it does, it should remove it and assign the removed space string to noSpace. But I have no idea how to remove spaces using only indexOf and substring. Can someone please help?

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

string.indexOf(" ") will return -1 if a space doesn't exist. Otherwise, it returns the index of the first instance of a space. So all you have to do is check if it returns something other than -1:

int index = s.indexOf(" ");
if(index != -1) {
}

Then cut the space out by taking the substring up to the space, and then from the index after the space:

noSpace = s.substring(0,index) + s.substring(index + 1);

That's it!

You get something like this:

String s = "space testing";
while(s.indexOf(" ") != -1) {
    s = s.substring(0, s.indexOf(" ")) + s.substring(s.indexOf(" ") + 1);
}

You just need to call indexOf() repeatedly until no more spaces are found, rebuilding the string with substring() after each space is (by joining the parts before and after the space).

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

  }

Result:
Before remmoveSpace:
After remmoveSpace:
Before remmoveSpace: StartWithEmpty
After remmoveSpace:StartWithEmpty
Before remmoveSpace:There are a few spaces separated by letters
After remmoveSpace:Thereareafewspacesseparatedbyletters
Before remmoveSpace:There Are2SpacesConsessConsecutively
After remmoveSpace:ThereAre2SpacesConsessConsecutively

You cut and append string removing white space

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM