2015-02-10 61 views
0

我有類似以下的方法。我有兩個數組列表。如果匹配成功,則刪除匹配的單詞並返回其餘的單詞

第一個數組列表包含:品牌名稱。

如:Hermanos的

第二個數組列表包含:產品名稱後面的品牌名稱。

如:Hermanos的嬰兒洋蔥

我想從產品陣列列表中刪除的品牌和無品牌的產品回報名。例如:洋蔥

爲此,我創建了以下代碼。但我很難這樣做。我評論過這個地方。

public void checkArrayListStatus(ArrayList getBrandName, ArrayList getProductName) { 
     log.debug("The Size of Brand Name ArrayList : " + getBrandName.size()); 
     log.debug("The Size of Product Name ArrayList : " + getProductName.size()); 


     for (int i = 0; i < getBrandName.size(); i++) { 
      String brandName = getBrandName.get(i).toString(); 
      String productName = getProductName.get(i).toString(); 
      if (productName.contains(brandName)) { 
       System.out.println("Matched"); 
       // if matching true, remove the matching word and return the rest of word 
      } else { 
       System.out.println("Didnot Match !!"); 
      } 
     } 
    } 
+0

你可以使用String的substring()函數。 – Beko 2015-02-10 16:48:13

+1

@Beka問題是,雖然一些產品有這樣的名字。例如:Hermanos嬰兒洋蔥。有些包含這樣的產品。例如:BBB最佳草莓果凍。這是BBB Best的品牌名稱。 – 2015-02-10 16:50:03

回答

1
if (productName.contains(brandName)) { 
     System.out.println("Matched"); 
     productName = productName.replace (brandName, ""); 
} else { 
     System.out.println("Didnot Match !!"); 
} 
+3

'productName.replace(brandName,「」 );'是NOP – xehpuk 2015-02-10 16:53:08

+0

正如@xehpuk提到的那樣,該行是一個NOP,它什麼也不做,所以要麼刪除它,要麼執行'productName = productName.replace(brandName,「」);' – Ascalonian 2015-02-10 17:24:57

+0

代碼行已被修改,謝謝 – Anto 2015-02-11 10:26:20

-1
return productName.replace (brandName, ""); 

這將通過一個空字符串替換名優產品,基本不除去它。

我也建議從

ArrayList 

改變參數類型

ArrayList <String> 

。那麼你也可以離開toString轉換。

+0

[格式](http://stackoverflow.com/help/formatting)很重要。你從「ArrayList」到「ArrayList /」'是什麼意思? – ryanyuyu 2015-02-10 16:50:15

+0

不能得到比令牌更小的工作... Html :(。你知道如何去做那個嗎? – flaghacker 2015-02-10 16:51:16

+0

明白了嗎? – flaghacker 2015-02-10 16:56:41

1

您可以在java中使用replace執行此任務。

看看這個。

productName.replace (brandName, ""); 
1

您可以使用replace達到此目的。嘗試下面提到的行。

productName.replace (brandName, "");