We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › .equals() use in a string with sentences
Page Index Toggle Pages: 1
.equals() use in a string with sentences (Read 2340 times)
.equals() use in a string with sentences
Jun 10th, 2010, 9:44am
 
Hello,

I'm new at this, I'm doing a basic text analysis of the play 'Julius Caesar' by Shakespeare.

Because it is a play the text looks like this:
CAESAR: sentences of caesar.
BRUTUS: sentences of brutus.

I have 2 strings that I use to analyze the text: one is called 'name', it searches for the names of characters within the text (like CAESAR, BRUTUS) and the other one is called 'phrase', which looks for the sentences only.

For the analysis I use name.equals("CAESAR") to search for the names, which works perfectly. But when I try this within the other string nothing happens. So something is wrong with using phrase.equals("something") . I tried searching for a whole sentence in the phrase string and that did work, but I want it to find one word in that sentence.

I hope someone can help! (the project deadline is tomorrow (: )

Thanks!

Re: .equals() use in a string with sentences
Reply #1 - Jun 10th, 2010, 10:07am
 
If you need to search for an occurrence of a word in a sentence, there are several methods in the String class that can help. The simplest is probably contains(). For example,

Quote:
String quote = "You blocks, you stones, you worse than senseless things!";

boolean hasYou = quote.contains("you");
boolean hasBlock = quote.contains("block");
boolean hasJuliet = quote.contains("Juliet");

println("Contains 'you': "+hasYou);
println("Contains 'block': "+hasBlock);
println("Contains 'Juliet': "+hasJuliet);


A few things to watch out for though with this simple example:


1. This example is a case-sensitive match, so 'you' would not, for example, match with 'You'. If you want to do a case insensitive search, convert the sentence  and the keywords into lowercase using the toLowercase()

2. This example would not count the number of matches if there is more than one occurrence in a single sentence. If you need to do that, you are probably best splitting the sentence into tokens.

3. This example would match matches within longer words (e.g. it would match 'you' with 'youth'. Again, the best way to avoid this would be to break the sentence down into individual words with split() and then do an equalsIgnoreCase() on each word.

I would recommend looking at the String API documentation for a longer list of possibly useful methods.
Re: .equals() use in a string with sentences
Reply #2 - Jun 10th, 2010, 11:16am
 
Thanks for the fast response! I tried using the contains() function but I get the error message "Cannot invoke contains(String) on the array type String[].

I also tried splitTokens(), which works fine! But if I use equals() on the string, it doesn't find anything!
Re: .equals() use in a string with sentences
Reply #3 - Jun 10th, 2010, 12:29pm
 
Quote:
Thanks for the fast response! I tried using the  contains() function but I get the error message "Cannot invoke contains(String) on the array type String[].


You are trying to use contains with an array of Strings rather than each element of the array. Something like this will work
Code:

String[] lines = loadStrings("Fileoftext.txt");
for(int i = 0; i < lines.length; i++){
 if(lines[i].contains("BRUTUS"))
  println(lines[i]);
}


and the following would create the error you experienced
Code:

lines.contains("BRUTUS")
Re: .equals() use in a string with sentences
Reply #4 - Jun 10th, 2010, 1:10pm
 
Ah, I see the problem. This does work, but for my program I really need to search this ArrayList for words. Is that possible? Or is it possible to convert the list to a string?
Re: .equals() use in a string with sentences
Reply #5 - Jun 10th, 2010, 1:25pm
 
All works as expected here...

Quote:
ArrayList words = new ArrayList();

void setup(){
  words.add("This sentence contains penguins.");
  words.add("This sentence contains rats.");
  words.add("This sentence is about lobsters.");
  println("Entire match tests:");
  for (int i=0;i<words.size();i++){
    println(((String)words.get(i)).equals("This sentence contains rats."));
  }
  println("Single word tests:");
  for (int i=0;i<words.size();i++){
    println(((String)words.get(i)).contains("contains"));
  }
}
Page Index Toggle Pages: 1