Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中捕获第一段并以字符串形式存储_Java_String_Core_Apache Stringutils - Fatal编程技术网

如何在java中捕获第一段并以字符串形式存储

如何在java中捕获第一段并以字符串形式存储,java,string,core,apache-stringutils,Java,String,Core,Apache Stringutils,我必须捕获第一段字符,并需要存储在字符串中。如果该段包含300个字符以上,我们只能捕获300个字符 我写了一些代码来做同样的事情,但是如果一个段落包含300个,或者它试图从第二个段落中捕获,它就会捕获。我只需要抓住第一段 String description = "Getting value from some paragraph as description"; if(StringUtils.isNotEmpty(description) && description !=

我必须捕获第一段字符,并需要存储在字符串中。如果该段包含300个字符以上,我们只能捕获300个字符

我写了一些代码来做同样的事情,但是如果一个段落包含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个字符。

试试这个

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));

如果段落为“r\n\r\n\”,则仅基于此拆分
split
。如果长度超过300,则子字符串是否有任何原因导致您不能简单地将段落中的前300个(或更少)字符子字符串?