2016-06-14 6 views
2

어디서나 배치 할 수있는 특정 String 문자열을 분할해야하며 (이 문자열이 동시에 여러 번 나타날 수 있음) 추출물을 추가하여 전체 문자열을 재구성해야합니다. StringBuffer.문자열에서 동일한 문자열 double을 추출하십시오.

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 결과가 비어 있기 때문에 실제로는 끝 부분에 한 번 추가됩니다. 실제로 테스트했습니다. 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

그리고 만약 당신이 target과 특별한 것을하고 싶다면 result [i] .equals (target); – dustinroepsch

+0

@ ΦXoce Smiling Пepeúpa, 안녕하십니까. 답장을 보내 주셔서 대단히 감사합니다. 그러나 대상 문자열은 문자열에있을 수 있으므로 더 복잡합니다. /. 나는''indexof''와''substring'' 사이에 좋은 조합이 필요하다고 생각합니다. 업데이트 된 질문을 참조하십시오. 감사합니다 :) – ulquiorra

관련 문제