找到你要的答案

Q:How get a whole word with just a part of it?

Q:如何得到一个完整的词只是一部分呢?

Example: I have a String like this:

String query = "....COD_OP = 1 AND USER_DATA_SIGNIN = ...."

I need to get the whole word ("USER_DATA_SIGNIN") when it have the "_DATA_" part.

In java is possible use some kind of substring inversely ? In this case I don't know how to get the "USER" part.

Example: I have a String like this:

String query = "....COD_OP = 1 AND USER_DATA_SIGNIN = ...."

我需要得到全世界(“user_data_signin”)时,有“_data_”部分。

在java可能使用某种串成反比?在这种情况下,我不知道如何得到“用户”部分。

answer1: 回答1:

Simple imlementation, null checks are left to you:

    for (String string : query.split(" ")) {
        if(string.contains("_DATA_"))
        {
            System.out.println(string); // USER_DATA_SIGNIN
            System.out.println(string.split("_")[0]); // USER
        }
    }

简单的实现,空支票给你:

    for (String string : query.split(" ")) {
        if(string.contains("_DATA_"))
        {
            System.out.println(string); // USER_DATA_SIGNIN
            System.out.println(string.split("_")[0]); // USER
        }
    }
answer2: 回答2:

You can use Pattern/Matcher classes which are responsible to regex mechanism in Java. You can create pattern which will represent word which have a-zA-Z0-9_ characters (which can be represented by \w character class) before and/or after it like

Pattern p = Pattern.compile("\\w*_DATA_\\w*");
Matcher m = p.matcher(text);
while(m.find()){
    System.out.println(m.group());
}

你可以使用模式/匹配负责在java正则表达式机制类。您可以创建模式,将代表词有a-za-z0-9_字符(可以由\w字符类为代表)之前和/或之后,它像

Pattern p = Pattern.compile("\\w*_DATA_\\w*");
Matcher m = p.matcher(text);
while(m.find()){
    System.out.println(m.group());
}
java  string  substring