?
Solved

JavaScript Equivalent of VBs StrComp

Posted on 2003-03-12
7
Medium Priority
?
1,110 Views
Last Modified: 2007-11-27
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
Comment
Question by:dtrick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8122229
Are you worried about case sensitivity?

Fritz the Blank
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 340 total points
ID: 8122266
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
 
LVL 3

Expert Comment

by:DeAn
ID: 8122509
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Expert Comment

by:DeAn
ID: 8122548
forget that last post please ;)
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8123114
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
 
LVL 3

Expert Comment

by:DeAn
ID: 8123129
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
 
LVL 4

Expert Comment

by:nax
ID: 8126070
or simply...

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

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question