Java 如何根据关键字/模式将一行文本拆分为多行?
我有一个StringBuffer,它包含以前文件中的多行内容,所有内容都基于一个较大行的模式合并在一起以进行比较。在进行更改之后,我基本上需要撤消将这些行合并在一起以重新写入一个新的StringBuffer,从而重新写入原始文件 例如,该文件包含如下行(全部在一行上): 我需要将该行重新写入多行(只要有字符串“macro(continued)”,就将其拆分): 如果有帮助的话,下面是我的代码片段,它完成了将原始文件放在多行中的第一部分,并将它们组合成组并写入新文件:Java 如何根据关键字/模式将一行文本拆分为多行?,java,stringbuffer,Java,Stringbuffer,我有一个StringBuffer,它包含以前文件中的多行内容,所有内容都基于一个较大行的模式合并在一起以进行比较。在进行更改之后,我基本上需要撤消将这些行合并在一起以重新写入一个新的StringBuffer,从而重新写入原始文件 例如,该文件包含如下行(全部在一行上): 我需要将该行重新写入多行(只要有字符串“macro(continued)”,就将其拆分): 如果有帮助的话,下面是我的代码片段,它完成了将原始文件放在多行中的第一部分,并将它们组合成组并写入新文件: StringBuff
StringBuffer sb = new StringBuffer();
Buffer = new FileReader(C:/original.txt);
BufferedReader User_File = new BufferedReader(Buffer);
String Line_of_Text;
while((Line_of_Text = User_File.readLine()) != null){
if(Line_of_Text.startsWith("macro ")){
sb.append("\n"+Line_of_Text);
}else if(Line_of_Text.startsWith("macro(continued)")){
sb.append(Line_of_Text);
}else{
sb.append(Line_of_Text+"\n");
}
}
BufferedWriter OutFile = new BufferedWriter(new FileWriter("C:/newFile.txt"));
OutFile.write(sb.toString());
OutFile.flush();
OutFile.close();
我一直在想办法做这件事,有什么建议/想法吗 我希望String类中的split()和contains()方法可以帮助您
Ref:问题是您有一个没有行的大字符串。您可以通过输入
System.out.println(line of_Text);
来测试行上方的内容(line of_Text.startsWith(“宏”){
。您可以看到哪里出了问题
我会将您的附加代码更改为:
String Line_of_Text = null;
while ((Line_of_Text = User_File.readLine()) != null) {
Line_of_Text = User_File.readLine();
String[] splitted = Line_of_Text.split("macro ");
for (String part : splitted) {
sb.append(part+"\n");
}
}
这将利用.split()
方法将字符串拆分为字符串数组。您可以使用该数组中的项来附加它们
编辑:我看到您有多个分隔符。.split()
使用正则表达式分隔符,因此您可以尝试:行/u文本.split(“macro | macro\\(continued\\)”;
您可以对给定标记使用字符串#split
来分割您的字符串
示例
String test = "'macro NDD89 Start;Load; shortDescription; macro(continued) stepone;steptwo,do stuff macro(continued) stepthree;more steps; do stuff macro(continued) finalstep'";
// // splits into String array, by
// // | followed by...
// // | | literal text
// // | | | double-escape on parenthesis, as
// // | | | this is a regex pattern
String[] splitted = test.split("(?=macro\\(continued\\))");
System.out.println("Raw:\r\n");
// prints the raw array (hard to read, as inline)
System.out.println(Arrays.toString(splitted));
// creates a list from the array and print each element one per line, trimmed
List<String> splittedList = Arrays.asList(splitted);
System.out.println("\r\nNice:\r\n");
for (String s: splittedList) {
System.out.println(s.trim());
}
最简单的解决方案
String x =oneLine.replace(" macro", "\nmacro");
快速修复QuakCore的答案
String breaker = "macro(continued)";
String x =oneLine.replace(" "+breaker, "\n"+breaker);
Raw:
['macro NDD89 Start;Load; shortDescription; , macro(continued) stepone;steptwo,do stuff , macro(continued) stepthree;more steps; do stuff , macro(continued) finalstep']
Nice:
'macro NDD89 Start;Load; shortDescription;
macro(continued) stepone;steptwo,do stuff
macro(continued) stepthree;more steps; do stuff
macro(continued) finalstep'
String x =oneLine.replace(" macro", "\nmacro");
String breaker = "macro(continued)";
String x =oneLine.replace(" "+breaker, "\n"+breaker);