2017-03-23 30 views
-1

我必须捕获第一段字符并需要存储在字符串中。如果段落包含超过300个字符,我们必须捕获300个字符。如何捕获第一段并在java中存储字符串

我写了一些代码来做同样的事情,但它捕获段落是否包含300或否则它会尝试从第二段捕获。我只需要捕捉第一段。

String description = "Getting value from some paragraph as description"; 
if(StringUtils.isNotEmpty(description) && description != null && description.length()>=300){ 
int maxValue = 300; 
description = description .substring(0, maxValue); 
schema.add(description); 
} else { 
if(StringUtils.isNotEmpty(description) && description != null && description.length()>=0){ 
schema.add(description); 
} 

任何人都可以建议我如何捕捉300个字符的第一段。

+0

如果一个段落是“R \ n \ r \ n \”然后就'split'基于此 –

+0

如果超过300则串 –

+0

是否有任何理由,为什么你不能简单的字符串的第300(或更少)字符从段落? –

回答

-1

试试这个。

String description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; 
System.out.println(description.substring(0, 300)); 
相关问题