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 › search string for word
Page Index Toggle Pages: 1
search string for word (Read 1756 times)
search string for word
Feb 11th, 2009, 5:05pm
 
hello all

I am a semi newbie to processing but love the program and want to know more.

I want to have processing search a string (and eventually multiple strings) for certain words then add that count to an int variable.  What kind of syntax would I use for that?

Ive searched around for how to do this but I'm not sure how to word it.

Thanks in advance for your help
Re: search string for word
Reply #1 - Feb 11th, 2009, 6:24pm
 
Code:
String foo="one two three four";
String bar="three four five";

if(foo.indexOf("two")>=0)
{
println("Foundin foo!");
}

if(bar.indexOf("two")>=0)
{
println("Found in bar!");
}
Re: search string for word
Reply #2 - Feb 11th, 2009, 7:34pm
 
That is awesome

Thank you JohnG
Re: search string for word
Reply #3 - Feb 11th, 2009, 8:10pm
 
What if I wanted to count the number of times that word appears in the string?
Re: search string for word
Reply #4 - Feb 11th, 2009, 9:00pm
 
Code:
String foo="one two one two one";
int index=0;
int count=0;
while(foo.indexOf("two",index)>=0)
{
count++;
index=foo.indexOf("two",index)+3; //search from after this occurance next time...
}
Re: search string for word
Reply #5 - Feb 12th, 2009, 11:15am
 
Another way is to split() (or using splitTokens()) the string (hoping it isn't megabytes in size!) then loop on the resulting array: for each array entry, get(word) on a HashMap. If null, put(word, 1). If not, put(word, previousCount + 1)
The HashMap will store the whole list of words as keys, with their count as value.

JohnG's method is perfect if you want the count of a precise word (or set of words) but a bit slow for a large set of words (as you walk the string for each occurrence), and you must know the words to search.
Re: search string for word
Reply #6 - Feb 13th, 2009, 4:43pm
 
How could I convert my Array into a Hashmap????
Re: search string for word
Reply #7 - Feb 13th, 2009, 5:12pm
 
Which array?
Re: search string for word
Reply #8 - Feb 13th, 2009, 5:36pm
 
ok sorry, here is what I have in my setup:

Code:

void setup() {
size(500, 200);
background(0);
noLoop();
smooth();
// load feed
//feedurl="http://en-us.fxfeeds.mozilla.com/en-US/firefox/headlines.xml";
feedurl="http://uncammac.wordpress.com/feed/";
println("Loading feed: "+feedurl);
feed=new FeedReader(feedurl);

// print feed data
println("Feed: "+feed.title);
println("------------------------------");
println("Description: "+feed.description);
println("Number of entries: "+feed.numEntries);
println("------------------------------");

// print feed entries
for(int i=0; i<feed.numEntries; i++) {
println(i+": "+feed.entry[i]);
// feed.entry[] stores strings url, description, and title
}
}


What is happening is that I am reading RSS Feeds through a FeedReader Class.  The feeds are stored in the array feed.entry[] but the data inside are strings.  I want to search a particular array spot (and the strings inside them) for a particular word then add the count of that word to a varible.  I see how I can use splitTokens() to split a string but dont know how to do it from within an array.

Thx again for your help
Re: search string for word
Reply #9 - Feb 13th, 2009, 5:48pm
 
Ah, so you need actually to find the number of occurrences of a given string inside a bigger string.
My hash map solution isn't really suited, then.
I have another brute force solution, with advantage of simplicity and no loop:
Code:
String s = "Inside tags, you will find multiple pieces of content.";

String[][] m = matchAll(s, "(e)");
println("Found " + m.length + " e's in the sentence");


Replace "(e)" with "(tag to search)" and s by feed.entry[i]) and you have your number.
Re: search string for word
Reply #10 - Feb 13th, 2009, 8:26pm
 
When I try

Code:

String[][] m = matchAll(feed.description[1], "(e)");
println("Found " + m.length + " e's in the sentence");


i get error:

the type of expression must be an array but it resolved to String

when I try
Code:

String[][] m = matchAll(feed.entry[1], "(e)");
println("Found " + m.length + " e's in the sentence");


i get error:

The Mechod matchAll(String,String) in the type PApplet is not applicable for the arguments (my_file_name.FeedEntry, String)

im not sure what I am doing wrong
Re: search string for word
Reply #11 - Feb 13th, 2009, 10:32pm
 
I don't know what your feed class looks like.
But from your code snippet, there is only one feed.description, so you cannot use [i] on it.
And feed.entry[i] looks like a class too, so you need to get a specific field inside, like feed.entry[i].description or similar.
Re: search string for word
Reply #12 - Feb 14th, 2009, 7:25pm
 
feed.entry[i].description is exactly what I was looking for.  Now Im searching for those strings fine and everything is working.  Thank you sooo much for your help.

youre the best
Page Index Toggle Pages: 1