[英]Obtaining a specific string from a line written in file
I have a file from which i read line by line and i have written a code for obtaining a certain portion of the string(from that line) which comes between two specific words eg我有一个文件,我从中逐行读取,并且我编写了一个代码来获取字符串的特定部分(从该行),它位于两个特定单词之间,例如
[abc] long time ago [cde]
Now i have written the following code for obtaining the string long time ago现在我很久以前写了下面的代码来获取字符串
if (line.contains("[abc]") && line.contains("[cde]")) {
int b = line.indexOf("abc");
int cc = line.indexOf("cde");
String tk = line.substring(b + 4, cc);
System.out.println(tk);
}
This code works fine but now the problem is that.i come across the following line这段代码工作正常,但现在的问题是。我遇到了以下行
[abc] long time ago [cde] [abc] Everyday is a new day [cde]
Now, just give me an idea how to obtain these two strings... because they come on a same line and my code just considers the first one...现在,请告诉我如何获取这两个字符串……因为它们在同一行,而我的代码只考虑第一个……
while (line.contains("[abc]") && line.contains("[cde]")) {
int b = line.indexOf("abc");
int cc = line.indexOf("cde");
String tk = line.substring(b + 4, cc);
System.out.println(tk);
line = line.substring(cc + 4);
}
Use regular expressions of this link as they will match every occurrence of a given String.使用此链接的正则表达式,因为它们将匹配给定字符串的每次出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.