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 & HelpPrograms › string comparison
Page Index Toggle Pages: 1
string comparison (Read 3623 times)
string comparison
Apr 22nd, 2005, 7:01pm
 
hey all,

i'm doing a little friday-afternoon parsing, but having problems comparing strings.  i'm loadStrings() and then looking for a particular line, but i can't get a match.

for example, i have a line in my .txt file that reads:
hello

but looping thru the String[] returned by loadStrings doesn't work:
Code:

String[] raw = loadStrings("sometext.txt");
for (int i=0; i<numLines; i++) {
if (raw[i] == "hello") { println("hello at: "+i); }
}


when i explicitly println the line i know "hello" to be on, it returns "hello".

i suspect it's a linefeed/carraige return issue, but i dunno how to get around that....any textheads out there?

thanks!
-depth
Re: string comparison
Reply #1 - Apr 22nd, 2005, 9:10pm
 
since strings are objects, using == will only compare the string objects against one another (are these two variables pointing at the same memory?) not their actual contents.

so what you need is to use "equals" or one of the other String methods:

if (lines[i].equals("hello")) // this will do it
Re: string comparison
Reply #2 - Apr 22nd, 2005, 9:13pm
 
hot damn.  thanks ben!  stuck in the actionscript mindset, i guess...
Re: string comparison
Reply #3 - Apr 23rd, 2005, 12:57am
 
yeah, it's certainly a bummer for people moving over from actionscript. it might be nice to change it in p5, though it'd break a lot of other things about how objects work, so it'd be too messy. i've added it to the faq since it comes up once every few months it seems.

hm, maybe we should start a faq section on moving from flash...
Page Index Toggle Pages: 1