• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1138
  • Last Modified:

JavaScript Equivalent of VBs StrComp

I know that a StrComp like function or method does not exist in in JavaScript.

StrComp(string1, string2[, compare])

string1 is less than string2 RETURNS -1
string1 is equal to string2 RETURNS 0
string1 is greater than string2 RETURNS 1

However, creating a function to do this in JavaScript should not be difficult. So rather than reinventing the wheel... I was wondering if anyone could show me some source code for this custom function.

Dtrick
0
dtrick
Asked:
dtrick
  • 4
  • 2
1 Solution
 
fritz_the_blankCommented:
Are you worried about case sensitivity?

Fritz the Blank
0
 
fritz_the_blankCommented:
This is case sensitive:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function strCompare(strFirst,strSecond){
     if(strFirst==strSecond){
          return 0;
     }
     if(strFirst < strSecond){
          return -1;
     }
     if(strFirst > strSecond){
          return 1;
     }
     
}
//-->
</SCRIPT>


</HEAD>
<BODY onLoad=alert(strCompare('ABC','BCD'))>

<P>&nbsp;</P>

</BODY>
</HTML>


Fritz the Blank
0
 
DeAnCommented:
function strComp(str1,str2,boolUseCase){
     var s1=str1, s2=str2
     if (boolUseCase) {
          s1 = str1.toUpperCase();
          s2 = str2.toUpperCase();
     }
    if(s1.length==""||s2.length=="")      return null;
    if(s1.length==s2.length)            return 0;
    if(s1.length<s2.length)           return -1;
    if(s1.length>s2.length)           return 1;
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DeAnCommented:
forget that last post please ;)
0
 
DeAnCommented:
this is an attempt to duplicate VB StrComp as closely as possible.

function strComp(string1,string2,compare){
   if(!string1 || !string2) return null ;
   if(!compare && compare != 0) compare = 1 ;
   if (compare == 1) {
      string1 = string1.toUpperCase() ;
      string2 = string2.toUpperCase() ;
   }
   if(string1 == string2) return 0 ;
   if(string1 < string2) return -1 ;
   if(string1 > string2) return 1 ;
}
0
 
DeAnCommented:
I should have gave you this if you want to test it...

<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function strComp(string1,string2,compare){
   if(!string1 || !string2) return null ;
   if(!compare && compare != 0) compare = 1 ;
   if (compare == 1) {
      string1 = string1.toUpperCase() ;
      string2 = string2.toUpperCase() ;
   }
   if(string1 == string2) return 0 ;
   if(string1 < string2) return -1 ;
   if(string1 > string2) return 1 ;
}
//-->
</SCRIPT>


</HEAD>
<BODY>
<br><a href="#" onClick="alert(strComp('abc','abc'))">strComp('abc','abc')</a>
<br><a href="#" onClick="alert(strComp('ABC','abc',1))">strComp('ABC','abc',1)</a>
<br><a href="#" onClick="alert(strComp('ABC','abc',0))">strComp('ABC','abc',0)</a>
<br><a href="#" onClick="alert(strComp('abc','bcd'))">strComp('abc','bcd')</a>
<br><a href="#" onClick="alert(strComp('xyz','XYZ',0))">strComp('xyz','XYZ',0)</a>
<br><a href="#" onClick="alert(strComp('abc','bcd',1))">strComp('abc','bcd',1)</a>
<br><a href="#" onClick="alert(strComp('','bcd',1))">strComp('','bcd',1)</a>
<br><a href="#" onClick="alert(strComp())">strComp()</a>

</BODY>
</HTML>
0
 
naxCommented:
or simply...

function strComp(str1, str2) {
   return str1>str2?1:(str1==str2?0:-1);
}
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now