0

I have a variable in Jquery like following:

   var var1="3com.rsmart.certification.impl.hibernate.criteria.gradebook.GreaterThanScoreCriterionHibernateImpl";

Now I have one more string like

 var var2="3com.rsmart.certification.criteria.impl.gradebook.GreaterThanScoreCriteriaTemplate";

Now i want to comapre both the string by considering upto certain stage

    3com.rsmart.certification.criteria.impl.gradebook.GreaterThanScoreCriteri

so can one help me out,

Thanks in advance.

Alexander Tokarev
  • 2,743
  • 2
  • 20
  • 21
Naresh Kallamadi
  • 163
  • 1
  • 14

3 Answers3

1

You can use substring.

http://www.w3schools.com/jsref/jsref_substring.asp

Example usage for your case

var compareLength = 10; // depends on your situation
if (var1.substring(0,compareLength)===var2.substring(0,compareLenth)) {
.
.
.
}

Why we use "===" is here. What is the correct way to check for string equality in JavaScript?

Also you can add a generic function to your client javascript page like this:

String.prototype.compareLeft=function(compareWith,len){
    return this.substring(0,len)===compareWith.substring(0,len)
}

And then in anywhere of your script (after defining function) you can use:

var compareLength = 10; // depends on your situation
if (var1.compareLeft(var2,compareLength)) {
.
.
.
}
Community
  • 1
  • 1
Gökçer Gökdal
  • 950
  • 1
  • 11
  • 18
0

Use javascript test method and regular expression, Try this

     var var1="3com.rsmart.certification.impl.hibernate.criteria.gradebook.GreaterThanScoreCriterionHibernateImpl";
    var var2="3com.rsmart.certification.criteria.impl.gradebook.GreaterThanScoreCriteriaTemplate";
    var patt = /3com.rsmart.certification.impl.hibernate.criteria.gradebook.GreaterThanScoreCriteri/i;
    if(patt.test(var1)==patt.test(var2)){
//your code
}
dhana
  • 6,487
  • 4
  • 40
  • 63
0

You can compare the two character by character.

var max = Math.min(left.length, right.length);
for (var i = 0; i < max; ++i) {
    if (left.charAt(i) != right.charAt(i))
        return left.substring(0, i);
return left.substring(0, max);
hsun324
  • 549
  • 3
  • 9