如何在java字符之前获取字符串

如何在java字符之前获取字符串,java,Java,我在active directory中看到了一个不规则的表达式,例如“description:John Newman loggin:03.26.2018 9:26:29”。单词的数量可以不同。我总是希望收到“登录”或“注销”,这一切都取决于输入数据 我试过: String a = Objects.toString(attrs.get("description"));//received values from active directory String b = a.spl

我在active directory中看到了一个不规则的表达式,例如“description:John Newman loggin:03.26.2018 9:26:29”。单词的数量可以不同。我总是希望收到“登录”或“注销”,这一切都取决于输入数据

我试过:

     String a = Objects.toString(attrs.get("description"));//received values from active directory
     String b = a.split(":")[1];
ааааа

John Newman Logged on

你能帮我吗?提前感谢。

据我所知,您正试图从主字符串中提取
登录:03.26.2018 9:26:29
信息

试试这个:

String s="description: John Newman Logged on: 03.26.2018 9:26:29";
int indx=s.indexOf("Logged on"); 
//will return the index of the substring which is 26 in this case. 
//I hope I didn't count wrong.
String log=s.substring(indx); 
//returns Logged on: 03.26.2018 9:26:29
编辑:仅提取登录的

登录的
(9)的长度是固定的,所以我们可以使用它来提取
它


当您知道要查找的字符串时,可以尝试以下方法

String s="description: John Newman Logged on: 03.26.2018 9:26:29";
String log1 = null;

if(s.toLowercase().contains("logged on")){
    log1 = "Logged on"; // Your desired string
}else if(s.toLowercase().contains("logged off")){
    log1 = "Logged off"; // Your desired string
}
或者干脆

String log1 = (s.toLowercase().contains("logged on")) ? "Logged on":(s.toLowercase().contains("logged off")) ? "Logged off" : null;

如果您认为登录的
也可能是用户名的一部分(子字符串),请使用边界。

请详细说明您到底想要什么?是否要获取登录的详细信息,而不考虑您收到的文字?完全正确。我对第二个冒号前的两个词感兴趣。在本例中,这是连接状态。您尝试了什么?请发布一段代码,并说明失败的地方。在询问问题字符串loggedOn=“loggeon”//之前,您是否尝试过任何操作总是收到“登录”谢谢你的帮助,但我只对“登录”感兴趣,没有日期和时间。我希望你明白了。我可能数错了。不确定。你的版本更正确。但是连接状态可以是“logged on”和“logged off”,所以我想知道是否可以在状态后面的冒号前面提取两个单词。我希望你能理解我。@Leo请看编辑@Harshita Sethi谢谢,我的朋友,它起作用了!
String log1 = (s.toLowercase().contains("logged on")) ? "Logged on":(s.toLowercase().contains("logged off")) ? "Logged off" : null;