如何显示固定长度的内容?

发布于 2024-12-11 07:38:59 字数 456 浏览 0 评论 0原文

我有一个像这样的字符串:

<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>. 

我想用 显示该字符串的一部分(只有内容,没有 HTML 标记)。我尝试了 substring(),但我不知道 html 标签在哪里完成,所以我的表单被破坏了。我只想得到大约 100 个字符。我该怎么做?

I have a string like this:

<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>. 

I want to display a part of this string with <h:outputText escape="false"> (just content, no HTML tag). I tried substring(), but I don't know where html tag finish, so my form is broken. I want to get about 100 character only. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温馨耳语 2024-12-18 07:38:59

我不确定我是否 100% 正确地理解了你的问题,但如果你要显示的所有字符串大致都是你的示例中的格式,那么你可以在没有 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;
  }
...
}

其中 text 变量包含像示例中那样的字符串。

I'm not sure if I understood your question 100% correctly but if all the strings you are to display are roughtly in the format from your example then you may display them without html and shortened to 100 characters this way:

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

And this is the 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;
  }
...
}

Where text variable contains string like in your example.

爱你是孤单的心事 2024-12-18 07:38:59

您可以使用以下 JSF HTML 标签输出文本:

<h:outputText escape="false" value="Your content here"  />

You can output text with the following JSF HTML Tag:

<h:outputText escape="false" value="Your content here"  />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文