[英]Get Substring from a String in Java
I have the following text: 我有以下文字:
...,Niedersachsen,NOT IN CHARGE SINCE: 03.2009, CATEGORY:...,
......,Niedersachsen,自2009年3月3日起不收费,类别:...,
Now I want to extract the date after NOT IN CHARGE SINCE:
until the comma. 现在我想在
NOT IN CHARGE SINCE:
之后提取日期NOT IN CHARGE SINCE:
直到逗号。 So i need only 03.2009
as result in my substring. 所以我只需要
03.2009
作为我的子串的结果。
So how can I handle that? 那我怎么办呢?
String substr = "not in charge since:";
String before = s.substring(0, s.indexOf(substr));
String after = s.substring(s.indexOf(substr),s.lastIndexOf(","));
EDIT 编辑
for (String s : split) {
s = s.toLowerCase();
if (s.contains("ex peps")) {
String substr = "not in charge since:";
String before = s.substring(0, s.indexOf(substr));
String after = s.substring(s.indexOf(substr), s.lastIndexOf(","));
System.out.println(before);
System.out.println(after);
System.out.println("PEP!!!");
} else {
System.out.println("Line ok");
}
}
But that is not the result I want. 但这不是我想要的结果。
You can use Patterns for example : 您可以使用模式,例如:
String str = "Niedersachsen,NOT IN CHARGE SINCE: 03.2009, CATEGORY";
Pattern p = Pattern.compile("\\d{2}\\.\\d{4}");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group());
}
Output 产量
03.2009
Note : if you want to get similar dates in all your String you can use while
instead of if
. 注意:如果您想在所有String中获得类似日期,则可以使用
while
不是if
。
Edit 编辑
Or you can use : 或者您可以使用:
String str = "Niedersachsen,NOT IN CHARGE SINCE: 03.03.2009, CATEGORY";
Pattern p = Pattern.compile("SINCE:(.*?)\\,");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group(1).trim());
}
You can use :
to separate the String
s. 您可以使用
:
来分隔String
。
String substr = "NOT IN CHARGE SINCE:";
String before = s.substring(0, s.indexOf(substr)+1);
String after = s.substring(s.indexOf(':')+1, s.lastIndexOf(','));
Of course, regular expressions give you more ways to do searching/matching, but assuming that the ":" is the key thing you are looking for (and it shows up exactly once in that position) then: 当然,正则表达式给你更多的方式做搜索/匹配,但假设“:”是你正在寻找的关键事情(在该位置恰好一次显示出来),那么:
s.substring(s.indexOf(':')+1, s.lastIndexOf(',')).trim();
is the "most simple" and "least overhead" way of fetching that substring. 是获取该子串的“最简单”和“最少开销”的方式。
Hint: as you are searching for a single character, use a character as search pattern; 提示:当您搜索单个字符时,请使用字符作为搜索模式; not a string!
不是一个字符串!
If you have a more generic usecase and you know the structure of the text to be matched well you might profit from using regular expressions: 如果您有一个更通用的用例,并且您知道要匹配的文本结构,那么您可以从使用正则表达式中获益:
Pattern pattern = Pattern.compile(".*NOT IN CHARGE SINCE: \([0-9.]*\),");
Matcher matcher = pattern.matcher(line);
System.out.println(matcher.group());
A more generic way to solve your problem is to use Regex to match Every group Between : and , 解决问题的一种更通用的方法是使用正则表达式匹配每个组之间:和,
Pattern pattern = Pattern.compile("(?<=:)(.*?)(?=,)");
Matcher m = p.matcher(str);
You have to create a pattern for it. 你必须为它创建一个模式。 Try this as a simple regex starting point, and feel free to improvise on it:
试试这个作为一个简单的正则表达式起点,并随意即兴:
String s = "...,Niedersachsen,NOT IN CHARGE SINCE: 03.2009, CATEGORY:....,";
Pattern pattern = Pattern.compile(".*NOT IN CHARGE SINCE: ([\\d\\.]*).*");
Matcher matcher = pattern.matcher(s);
if (matcher.find())
{
System.out.println(matcher.group(1));
}
That should get you whatever group of digits you received as date. 这应该可以获得您在日期收到的任何数字组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.