How to check if a string is contained in another?

1

I have several strings in a ArrayList . I would like to know how to return a string from this list that contains another string that was passed. For example, if I pass a String "Bean" , I want to return the string "AtendenteBean" .

    
asked by anonymous 15.02.2014 / 21:37

4 answers

5

Since you are using ArrayList to store your% s of% s, I would advise you to go through all the variables in your list and check if it contains the text you want. Example:

List<String> list = new ArrayList<String>();

//pega os dados do xhtml e joga na variavel list

for(String s: list) {
    if(s.contains("Bean")) {
        System.out.println(s);
    }
}

In the example above I'm printing all the% s of% s that have the literal String of the variable String and then printing it. Instead of printing you can manipulate the variable Bean the way you want, it contains the literal excerpt that you put in the list conditional above.

    
15.02.2014 / 22:36
1

I think you're confusing what a "String" is. Do not you want to say a class AttendantBean, which for example inherits from PessoaBean? If you are sure that an instance of PessoaBean is an AttendantBean, you can use a cast, see if the example below makes sense:

PessoaBean p = findPessoa(...);
if (p instanceof AtendenteBean) }
    String codAtendente = ((AtendenteBean) p).getCodAtendente();
}
    
15.02.2014 / 22:14
1

I was able to use regex .

Pattern pattern = Pattern.compile("\{(.+?)Bean");
Matcher matcher = pattern.matcher(text);

while (matcher.find()){
    beanName = matcher.group(1);
    beanName = beanName.concat("Bean");

    if (text.contains(beanName)) {
        break;
    }
}

It looks for words that contain Bean and start with the key { . If the text contains it, it breaks the loop.

    
15.02.2014 / 22:59
0

Dude, try to better elaborate your question, I could not understand exactly what you meant.

Anyway, I think what you want is something like objeto.getClass().getSimpleName() .

    
15.02.2014 / 21:52