2011-10-23 23 views
1

私はこのような文字列を持っている:私は<h:outputText escape="false">(コンテンツだけ、ないHTMLタグ)と、この文字列の一部を表示したい<h:outputText>固定長のコンテンツを表示する方法は?

<p><strong>Make sure you’re paying the right price for your household and trade services with pricing calculators from Australia’s largest online services marketplace, ServiceSeeking.com.au </strong></p>. 

を。私はsubstring()を試しましたが、どこにHTMLタグが終わったのかわからないので、フォームが壊れています。私は約100文字を取得したい。これどうやってするの?

答えて

1

は私が100%正しくあなたの質問を理解している場合はわからないが、あなたはすべての文字列を表示する場合のフォーマットでroughtlyありますあなたの例では、あなたは、HTMLせずにそれらを表示し、100文字に短縮この方法があります。

<h:outputText value="#{textExtractorBean.extractedText}"/> 

をそして、これはBeanです:

class TextExtractorBean{ 
... 
    getExtractedText(){ 
    Pattern pattern = Pattern.compile("<([a-z])+>"); 
    Matcher matcher = pattern.matcher(text); 

    int firstIdxAfterOpeningTags = 0; 
    while(matcher.find()){ 
     firstIdxAfterOpeningTags = matcher.end(); 
    } 

    pattern = Pattern.compile("</([a-z])+>"); 
    matcher = pattern.matcher(text); 

    int firstIdxBeforeClosingTags = text.length(); 
    if(matcher.find()){ 
     firstIdxBeforeClosingTags = matcher.start(); 
    } 

    String extractedText = text.substring(firstIdxAfterOpeningTags, 
      firstIdxBeforeClosingTags); 
    String shortenedText = extractedText.length() > 0 ? extractedText 
      .substring(0,100) : extractedText; 
    return shortenedText; 
    } 
... 
} 

テキスト変数には、例のような文字列が含まれています。

+0

ありがとう、私はそれをしました。 – xuanhung2401

0

次のことができ、次のJSF HTMLタグで出力テキスト:

<h:outputText escape="false" value="Your content here" /> 
関連する問題