2016-06-14 26 views
2

文字列を特定のStringで分割する必要があります(この文字列は複数同時に存在することがあります)。 StringBuffer複数の同じ文字列を文字列から抽出する

String targeted = "test" ; 
String plainString ="azertytestqwerty"; 

//desired outcome 

StringBuffer sb = new StringBuffer(); 
sb.append("azerty"); 
sb.append("test"); 
sb.append("qwerty"); 

-------------------------- 

String targeted = "test" ; 
String plainString ="a.test"; 

//desired outcome 

StringBuffer sb = new StringBuffer(); 
sb.append("a."); 
sb.append("test"); 

-------------------------- 

String targeted = "test" ; 
String plainString ="test mlm"; 

//desired outcome 

StringBuffer sb = new StringBuffer(); 
sb.append("test"); 
sb.append("mlm"); 

-------------------------- 

String targeted = "test" ; 
String plainString ="aaatestzzztest"; 

//desired outcome 

StringBuffer sb = new StringBuffer(); 
sb.append("aaa"); 
sb.append("test"); 
sb.append("zzz"); 
sb.append("test"); 

これを実行するために、任意の簡単な方法:追求する特定の文字列の場合は、例えば

鈍感でなければなりませんか?

同じ順序で、それはだ、私はこれを行う理由を

Pattern pattern = Pattern.compile(".*"+targeted +".*"); 
Matcher matcher = pattern.matcher(text); 
if (matcher.find()) 
{ 
    System.out.println(matcher.group(1)); 
} 

しかし、私は、文字列を抽出し、それらを追加する方法がわからない:

私はのような正規表現を使用する必要があると思いますplainStringがPOIを使用してExcelセルに追加されるため、ターゲットの文字列にフォントの色を追加する必要があるためです。

例:

XSSFRichTextString richString = new XSSFRichTextString(); 
richString.append("azerty"); 
richString.append("test", highlightFont); 
richString.append("qwerty"); 
cell.setCellValue(richString); 

は非常に

ありがとう
+0

このようにStringBufferに追加する目標は何ですか? "文字列全体を再構成する"の場合、実行するだけで何が問題なのですか? 'StringBuffer sb = new StringBuffer(plainString);' – dustinroepsch

+0

@Dustin Ryan-Roepsch。より精密に編集された投稿。ありがとうございます:) – ulquiorra

+0

Gotcha!それはもっと意味をなさない:) – dustinroepsch

答えて

2

ここでは、任意の組み合わせを試すことができます。

public String extractMultiples(String plainString, String targeted) { 
    StringBuffer sb = new StringBuffer(); 

    // split covers all occurrences in the beginning;empty element, and in 
    // the middle 
    String[] result = plainString.split(targeted); 
    for (int i = 0; i < result.length; i++) { 
     sb.append(result[i]); 
     if (i < result.length - 1)// not the last one 
      sb.append(targeted); 
    } 

    // in the end 
    if (plainString.endsWith(targeted)) 
     sb.append(targeted); 
    return sb.toString(); 
} 
+0

こんにちはお返事ありがとうございますが、対象の文字列は文字列のどこにでも置けるので、もっと複雑です。更新された質問をご覧ください。ありがとう:) – ulquiorra

+0

ありがとう、あなたは本当に私をたくさん助けました。どうもありがとうございます。私は苦労していた。さらにもう一つのケースは、plainStringがtargetedStringだけで構成される場合です。バッファはtargetedStringを2回追加します – ulquiorra

+0

@ulquiorra結果は空で、最終セクションに1回追加されますので、実際にはテストしません。 hhとあなたに感謝の言葉を言う最善の方法は、答えを投票し、それを右に呼び出すことです:)、幸運。 – TiMr

2

パターンのMatcherがあまりにも多分少しです...

は言葉だけでスプリットを行い、結果として得られる配列のforループを作成します。

public static void main(String[] args) { 
    String targeted = "test"; 
    String plainString = "azertytestqwerty"; 
    String[] result = plainString.split(targeted); 
    StringBuffer sb = new StringBuffer(); 
    for (int i = 0; i < result.length; i++) { 
     sb.append(result[i]); 
    } 
} 
+0

とあなたはターゲットと何か特別なことをしたい場合は、あなたがちょうどresult [i] .equals(target); – dustinroepsch

+0

@ΦXoceSmilingПepeúpa、こんにちはお返事ありがとうございますが、対象の文字列はStringのどこにでも置くことができますので、もっと複雑です。私は '' indexof''と '' substring''の間に良い組み合わせが必要だと思います。更新された質問をご覧ください。ありがとう:) – ulquiorra

関連する問題