FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   all splitstrings are created equal (or are they?)
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: all splitstrings are created equal (or are they?)  (Read 233 times)
Euskadi


all splitstrings are created equal (or are they?)
« on: Feb 24th, 2004, 9:26pm »

This code snippet should compare to String arrays and find matches... problem is nothing ever matches, even if the same string is split into two arrays.
 
So I added a println so I can see what the strings are and a second check to see if they are ==.
 
Code:

void setup(){
String str1 = "One;Two";
String[] numID = splitStrings(str1, ';');
String[] numEscStr = splitStrings(str1, ';');
int numEscNum[] = new int[2];
   for(int en = 0; en < numID.length; en++){
    for(int es = 0; es < numEscStr.length; es++){
 
    println("|" + numEscStr[es] + "|" + numID[en] + "|"); //show me the strings
    println(numEscStr[es] == numID[en]); // should sometimes be true
 
     if(numEscStr[es] == numID[en]){
   numEscNum[es] = en;
   println(es + " " + en);
 }
    }
  }
}

 
This is the output... am I missing something?
 
|One|One|
false  //HUH?
|Two|One|
false
|One|Two|
false
|Two|Two|
false //HUH?
 
 
TomC_
Guest
Email
Re: all splitstrings are created equal (or are the
« Reply #1 on: Feb 24th, 2004, 10:58pm »

Strictly speaking, in Java == tests the identity of objects.  The Strings you are comparing aren't the same object, so they aren't identical.  
 
String has a custom equals method to compensate for this.  Use string1.equals(string2) to test for lexicographic equality instead.
 
Euskadi


Re: all splitstrings are created equal (or are the
« Reply #2 on: Feb 25th, 2004, 2:32am »

arf. ok, I knew that at one time. thanks!
 
Pages: 1 

« Previous topic | Next topic »