从字符串创建一个句子数组 - Java Velocity Template

时间:2014-10-29 14:51:15

标签: java html velocity

我正在使用力度模板,需要将函数的输出拆分为句子数组:


$page.getSectionCopy()

输出:

The first sentence. The second sentence. The third sentence. The fourth sentence.The fifth sentence. 

我想要实现的目标如下:

<p>
The first sentence. The second sentence.
</p>
<p>
The third sentence. The fourth sentence. The fifth sentence. 
</p>

正如您所看到的,我需要获取前两个句子并将它们放在第一个<p>标记中。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果getSectionCopy()返回一个String,我很确定你可以这样做:

#set ($splitString = $page.getSectionCopy().split("\\.")) ## split takes regex - split on period.
<p>
    $splitString[0].trim().  $splitString[1].trim().
</p>
<p>
    #foreach ($sentence in $splitString)
        ## $velocityCount built in variable to count for index.  1 based, not 0 based.
        #if ($velocityCount > 2) 
            $sentence.trim().&nbsp;
        #end
    #end
</p>

Haven未对代码进行测试。

此外,这是一篇可能有用的相关文章:Using velocity split() to split a string into an array doesnt seem to work