1

I have a string "SünilGarg"

now I want to check if above string contains chars from second string "üG#$".

The one way is to check every single char of second string with first string using loop. But is there any other best and efficient way to do so ?

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189

6 Answers6

2

Try this one:

var mainString = "SünilGarg",
  charsString = "üG#$";

var contains = Array.prototype.some.call(mainString, function(char) { 
   return charsString.indexOf(char) !== -1;
});

Because strings are array like objects (partially), it's easy to use some array methods on them.

Dmitri Pavlutin
  • 18,122
  • 8
  • 37
  • 41
  • "Array like" is probably going too far. The closest concept is [*Array exotic object*](http://ecma-international.org/ecma-262/6.0/index.html#sec-array-exotic-objects), but the String length property doesn't quite cut it (e.g. you can't shorten a string by setting its length to some lower value). I expect any Array method that modifies the string won't work (e.g. *push*, and Strings have their own *slice* method). – RobG Mar 14 '16 at 13:23
  • @RobG Agreed. Strings are `partially array like objects`. :) – Dmitri Pavlutin Mar 14 '16 at 13:26
1

The below code should work

RegExp.escape= function(s) {
  return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};

var fString = 'SünilGarg';
var sString = RegExp.escape('üG#$');
var pattern = new RegExp('['+ sString +']');
pattern.test(fString); //Will return true
blessanm86
  • 31,439
  • 14
  • 68
  • 79
1

I'm pretty sure the fastest way is to loop over the larger string and for each character check if the shorter string contains it:

function containsAny(haystack, needles){
 for(var i = 0; i < haystack.length; i++){
  for(var j = 0; j < needles.length; j++){
   if(haystack[i] === needles[j]) {
    return true;
   }
  }
 }
 return false;
}

Here's some evidence: http://jsperf.com/stringcontainsany

EDIT:

Actually, string.indexOf is faster than indexing into the string, so this is more efficient:

function containsAny(haystack, needles) {
  for (var i = 0; i < haystack.length; i++) {
    if (needles.indexOf(haystack[i]) !== -1) {
      return true;
    }
  }
  return false;
}
Steve Ruble
  • 3,875
  • 21
  • 27
  • The test isn't conclusive since a match will be found on the first character of the second loop, so only tests five characters. Checking shorter on longer must be faster (in this case) since a match will be found on on the second character of the first loop (i.e. it would only test two characters). Such short tests mean that cases where the setup is expensive don't have time to recover the cost (if they can). – RobG Mar 14 '16 at 13:52
0

I'm not the brightest in this case but I'd go with one loop and indexOff. eg:

function isMatch(str) {
  var matchStr = 'üG#$';
  for (var i = 0; i < matchStr.length; i++) {
    if (str.indexOf(matchStr[i]) !== -1) {
      return true;
    }
  }
  return false;
}

Not sure if there are any smarter moves, but I think regex might be slower.

Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
0

Use indexOf to check if there is a character match

function checkString(myString,withCompare){
 var dumArray = withCompare.split('')
 var i = 0;
 for(i;i<dumArray.length;i++){
   if(myString.indexOf(dumArray[i]) ==-1){
      // do nothing
      console.log('nothing')
   }
    else{
      console.log(dumArray[i])
    }
 }


}

checkString('hello','mvo');

jsfiddle

brk
  • 48,835
  • 10
  • 56
  • 78
0

Using an es6 one liner:

var str = 'SünilGarg';
var match = 'üG#$';

var res = match.split('').some(v => str.includes(v));
console.log(res);

Note that this will work in modern browsers like newest chrome or firefox only

Or for wider support, using es5:

var res = match.split('').some(function (v) {
  return str.indexOf(v) > -1;
});
baao
  • 71,625
  • 17
  • 143
  • 203