6

How can I somehow split/separate my JavaScript variable by comma (,).
And then check if value-of-any-of-the-separated-strings = "something"

For example, my variable has the value 1,2,3,4,5,6,7,8,9,10,2212312, and I want to check if any of the numbers are = 7 in a IF-Statement.

Does anyone have any ideas how this can be done?

SethO
  • 2,703
  • 5
  • 28
  • 38
Mark Topper
  • 249
  • 1
  • 3
  • 14

8 Answers8

19

First, split the string by ",". Then, use indexOf on the split-string array to see if the target string is found (-1 means it wasn't found in the array). For example:

var str = "1,2,3,4,5,6,7,8,9,10,10,2212312";
var split_str = str.split(",");
if (split_str.indexOf("7") !== -1) {
    // Original string contains 7
}

References:

Ian
  • 50,146
  • 13
  • 101
  • 111
6

This is a simple application of Array.prototype.some:

var yourVar = '1,2,3,4,5,6,7,8,9,10,2212312';
function isSeven(val) {
    return val === '7';
}
if (yourVar.split(',').some(isSeven)) {
    //do stuff
}

Another common way this could be written is:

if (~yourVar.split(',').indexOf('7')) {
    //do stuff
}

Or if Array.prototype.contains has been defined:

if (yourVar.split(',').contains('7')) {
    //do stuff
}

Or if you want to use a regular expression:

if (/(?:^|,)7(?:,|$)/.test(yourVar)) {
    //do stuff
}

Note: Array.prototype.some, Array.prototype.indexOf and Array.prototype.contains all require polyfills to work correctly cross browser.

Community
  • 1
  • 1
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
1

Split it into an Array, then use indexOf to see if it's there. If it returns -1, it isn't.

"1,2,3,4,5,6,7,8,9,10,2212312".split(",").indexOf("7")
Bartek
  • 15,269
  • 2
  • 58
  • 65
1

man i hope it will help you.

    var yourValues = '1,2,3,4,5,6,7,8,9,10,2212312';
    var array = yourValues.split(",");
    boolean isValue = false;
    for(i in array)
    {
       if(array[i]=='7')
       {
             isValue=true;
       }
    }


    if(isValue)
        alert("your number is in the string");
    else
        alert("your number is in the string");
Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82
0

Use split and Array.indexOf()

var str = "1,2,3,4,5,6,7,8,9,10,2212312";
var num = 7;
var pieces = str.split(",");
var index = pieces.indexOf(num.toString());

It can be done with regular expressions too

var str = "1,2,3,4,5,6,7,8,9,10,2212312";
var num = 7;    
var re = new RegExp("(^|,)" + num + "($|,)");
alert(re.test(str));

jsFiddle example

epascarello
  • 204,599
  • 20
  • 195
  • 236
0

use split along with indexOf:

var someString = '1,2,3,4,5,6,7,8,9,10,2212312';
var splitArray = someString.split(',');
var sevenPosition = splitArray.indexOf('7');

http://jsfiddle.net/jbabey/f4NLY/

jbabey
  • 45,965
  • 12
  • 71
  • 94
0

You could use Array.filter, something like:

var values = '1,2,3,4,5,6,7,8,9,10,2212312'.split(','), find = 7;
if ( values.filter(function(a){return +a === find;}).length ) { /* ... */ }
KooiInc
  • 119,216
  • 31
  • 141
  • 177
-2

Are you looking for the "contains" function. You can use jQuery for this.

if ($.inArray(7, value-of-any-of-the-seperated-strings))
{
   console.log("there is a 7!")
}
Carlos Martinez T
  • 6,458
  • 1
  • 34
  • 40
  • 2
    Though it should be mentioned if the OP is not already using jQuery there's no need to include such a heavy library for something that's [native to javascript](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf). – Brad Christie Oct 05 '12 at 14:07