簡體   English   中英

如何從字符串中刪除空格? 僅使用indexOf()和substring()

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

這是我到目前為止所寫的內容。 它檢查是否存在空間。 如果是,則應將其刪除並將刪除的空格字符串分配給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;         
}      

如果空格不存在, 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);
}

您只需要重復調​​用indexOf()直到找不到空格,在每個空格之后用substring()重建字符串(通過連接空格前后的部分)。

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

  }

結果:
在remmoveSpace之前:
在remmoveSpace之后:
在remmoveSpace之前:StartWithEmpty
在remmoveSpace之后:StartWithEmpty
在remmoveSpace之前:有一些空格用字母分隔
在remmoveSpace之后:Thereareafewspacesseparatedbyletters
在remmoveSpace之前:還有Are2SpacesConsessConsecutive
remmoveSpace之后:ThereAre2SpacesConsessConsecutively

你剪切並追加刪除空格的字符串

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM