从字符串中拉取标题 - Java
想知道,从字符串中的标题拉取信息的最佳方法是什么。从字符串中拉取标题 - Java
我有一个类和getter方法的标题(摘要,标题1,标题2)。
例如,
如果非要等于字符串,
This: is the first line of a String: xx-string: This is a long String
Summary:
This is the summary of the String
Heading 1:
This is the first heading
Heading 2:
This is another heading
什么是设置字符串的值以期望的方式,
摘要,标题1,标题2
到
Summary = This is the summary of the String
Heading 1 = This is the first heading
Heading 2 = This is another heading
谢谢!
编辑
这里是我要去的,
public void setHeadings(Data fileData) {
String description = fileData.getDescription();
//String [] headings = description.split(":");
int indexOf;
indexOf = description.indexOf("Summary");
if(indexOf != -1)
{
String subString = description.substring(indexOf);
int indexOfNextHeading = subString.indexOf(":");
if(indexOfNextHeading != -1)
{
System.out.println(indexOf + ":" + indexOfNextHeading);
setSummary(description.substring(indexOf,indexOfNextHeading-1));
System.out.println(description.substring(indexOf,indexOfNextHeading));
}
}
}
也就是说然而,吐出来的数组越界异常。
使用扫描仪对象。
import java.util.Scanner;
然后用它一次读取一行字符串。
Scanner sc = new Scanner(string);
sc.useDelimiter("\n"); // read one line at a time
现在sc.hasNext()告诉你是否还有行要读,而sc.next()返回下一行。使用它一次迭代字符串一行。您可以测试每一行以查看它是否等于“Summary:”或“Heading 1:”等。然后,您可以使用StringBuffer来添加要创建的每个String的每一行。
如果你愿意,我可以为你写,但它很简单。
感谢您的回答!我现在就试一试。 – TomSelleck 2012-07-11 14:45:41
我现在只想试试这个,如果说“Summary”有多行信息,我该怎么办?就像我怎么知道什么时候停止阅读某一特定标题的内容? – TomSelleck 2012-07-11 15:00:07
我会继续将每一行追加到StringBuffer,直到您找到一行显示它必须结束的行。如果你的格式总是为“Summary:somelines Heading 1:somelines”,那么你可以直到找到等于“Heading 1:”的行。 – correcthorsebatterystaple 2012-07-11 15:02:05
摘要和标题是多行段落吗?第一行怎么样?我们是否完全放弃第一行? – rmarimon 2012-07-11 14:25:58
听起来像作业...? – carlspring 2012-07-11 14:32:14
不做作业,我是实习生。第一个包含文本的框是** 1 **字符串 – TomSelleck 2012-07-11 14:33:18