How do I compare two string values?

Discussion in 'JavaScript' started by Imozeb, Mar 24, 2010.

  1. #1
    I have two string variables and I want to compare them, how do I do this?
    I've been trying something like this (I have to use slice for my real code).


    Simplified Javascript Code:
    
    str1 = 'Hello World';
    var1 = str1.slice(0,5);
    var2 = 'peaches';
    if(var1 != var2)
    {
      //execute some code
    }
    
    Code (markup):
    Thanks!

    ~imozeb :)
     
    Imozeb, Mar 24, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    You have written right code. And what doesn't work in the code?
     
    s_ruben, Mar 24, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well... my code is slightly more complicated. About 200 lines of functions but the main problem is as stated above. It looks more like this.

    Javascript Code:
    
    check1 = function()
    {
      var2 = var1;
      if(var1 != var2)
      {
        //execute some code
      }
    }
    getcode = function()
    {
       str1 = xmlhttpget.responseText;  //Could be about any 20 letter char plus 2 spaces (Hello World  )
    //delete 2 spaces
       var1 = str1.slice(0,5-2);
    }
    
    
    Code (markup):
    I checked the lengthes and they are the same, I also checked the output and it looks the same but when I compare them it does not work.

    Thanks!

    If it still works for you tell me and I'll try to show you the full code.

    ~imozeb
     
    Imozeb, Mar 25, 2010 IP
  4. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #4
    But if var2 = var1;, how can this work

    if(var1 != var2)
    {
    //execute some code
    }
    ??

    I don't understand you.
     
    s_ruben, Mar 25, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Oh! Sorry, I was busy when I wrote that. This is what the code should look like:

    Javascript code:
    
    //name can be anything (it is an input text box)
    getname = function()
    {
       check1('world')
    }
    check1 = function(name)
    {
    //get input name
      var2 = name;
    //make sure name does not equal to the response text
      if(var1 != var2)
      {
        //if so execute some code
      }
    }
    //get responseText
    getcode = function()
    {
       str1 = xmlhttpget.responseText;  //Could be about any 20 letter char plus 2 spaces (Hello World  )
    //delete 2 spaces
       var1 = str1.slice(0,5-2);
    }
    
    Code (markup):
     
    Imozeb, Mar 25, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    And what doesn't work in this code? Alert the values of var1 and var2 before if(var1 != var2).
     
    s_ruben, Mar 26, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I thought I tried that debug along with the .length debug to check for spaces but for some reason when I tried it again this time I found the error. There was a hidden space before one of the texts and the ending letter of one of the texts was chopped off.

    Thanks!

    ~imozeb :)
     
    Imozeb, Mar 26, 2010 IP