2016-04-28 1 views
-2

ArrayList内の要素が "_bp"で終わっていないかどうかを確認してください。 ArrayListをStringに変換する必要がないのは素晴らしいことです。配列リストの要素の文字列が "_bp"で終わっているかどうかを確認するには

私はコードの下に試してみました:

ArrayList<String> comparingList = new ArrayList<String>(); 

String search = "_bp"; 
for (String str : comparingList) 
{ 
    if (str.trim().contains(search)) 
    { 
     // Do Nothing 
    } 
    else 
    { 
     str.trim().concat("_bp"); 
     //Replace existing value with this concated value to array list 
    } 
} 

しかし、私はまだ、既存の要素に「_bp」を追加することはできませんよ、また、文字列の中に、私の comparingListを変換したいいけません。

可能であれば、小さなコードを使用することをお勧めします。予め おかげ:)

+1

なぜuse''String.endswith( "_ BP"); '? –

答えて

0
String a = "test"; 
System.out.println("1 " + a.concat("_bp")); 
System.out.println("2 " + a); 
a = a.concat("_bp"); 
System.out.println("3 " + a); 

出力:このような

1 test_bp

2試験

3 test_bp

コード:

+0

文字列検索= "_vw"; \t \t \t \t int i = 0; (文字列str:comparingList)用 \t \t \t \t \t \t \t \t \t { \t \t \t \t \t \t場合(str.trim())(検索を含む)の \t \t \t \t \t \t \t { \t \t \t \t \t \t \t}他 \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t comparingList.set(I、str.trim()CONCAT( "_ VW")。)。 \t \t \t \t \t \t \t} \t \t \t \t \t \t I ++。 \t \t \t \t \t} – Rahul

+0

は、これは働いたが – Rahul

+0

申し訳ありません置き換える「_bpは、」私は私のコードを変更するかどうかわからない:) – Rahul

0
 ArrayList<String> comparingList = new ArrayList<String>(); 
     comparingList.add("test1_bp"); 
     comparingList.add("test2"); 
     String search = "_bp"; 
     for (int i = 0; i < comparingList.size(); i++) { 
      String tmpStr = comparingList.get(i); 
      if(tmpStr.contains(search)){ 
       //do nothing 
      }else{ 
       comparingList.set(i, tmpStr+"_bp"); 
      } 
     } 
     System.out.println(comparingList); 

これは働いた:)

String search = "_bp"; 
int i = 0; 
for (String str : comparingList) 
{ 
    if (str.trim().contains(search)) 
    { 

    } 
    else 
    { 
     comparingList.set(i, str.trim().concat("_bp")); 
    } 
    i++; 
} 
関連する問題