How to check if one string is contained in another?

Asked

Viewed 3,813 times

1

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

  • Dude, it’s still really messed up. What exactly can come of this String Bean? What kind of code do you expect in response? What is the format of this xhtml? Besides AtendenteBean, what else is String could it be? You have a class AtendenteBean? If yes, how is this class?

  • Dude, I want this, I’m gonna pass a piece of a String, Bean for example, and returns me AtendenteBean. I extract the text from inside the page xhtml. It would be like a search in sql using like`.

  • 1

    Eu extraio o texto de dentro da página xhtml, extract and store where in your code? An array, an Arraylist? Maybe you should leave that part of xhtml aside in your question, since that information doesn’t seem relevant to anyone giving the answer you need

  • @Math I store in a structure with ArrayList

  • Look, the String class has the method contains(). As far as I understand, this is what you want...http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#contains%28java.lang.Charsequence%29

  • Okay, it’s going to be really big, but it’s okay. Imagine that this is a String. Este texto contem algumas palavras de dúvida de alguém que queira saber pelos usuários do stackOverflow como resolver sua dúvida. And there, I want for example to pass the String life, and return me the word dúvida. They understood?

Show 1 more comment

4 answers

5

Whereas you’re wearing one ArrayList to store their Strings I would advise you to go through all the variables in your list and check if it contains the desired text. 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 above example I am printing all the Strings that have the literal Bean of the variable list and then printing it. Instead of printing you can manipulate the variable s as you wish, it contains the excerpt from the literal that you put on probation if above.

1

I think you’re confusing what a "String" is. Don’t you mean an Atendentebean class, which for example you inherit from Pessoabean? If you are sure that an instance of Pesoabean is an Attendant, you can use a cast, see if the example below makes sense:

PessoaBean p = findPessoa(...);
if (p instanceof AtendenteBean) }
    String codAtendente = ((AtendenteBean) p).getCodAtendente();
}
  • i am not confusing, just want to pick up a text that is inside a page, passing a part of this text.

1

I managed using 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;
    }
}

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

0

Man, try to elaborate on your question, I couldn’t understand exactly what you meant.

Anyway, it seems to me that what you want is something like objeto.getClass().getSimpleName().

  • really missed me putting that Bean is a part of a string

Browser other questions tagged

You are not signed in. Login or sign up in order to post.