0

I have a variable [ITEM_NAME] that stores an item name from a shopping cart. I want to check whether the value in [ITEM_NAME] is like one of the following strings:

"CHICAGO NEW"
"CHICAGO OLD"
"CHICAGO TEXT"
"CHICAGO PURE"

In pseudocode:

if ([ITEM_NAME].contains("CHICAGO"))

I want a condition that will be satisfied for all strings that start with "CHICAGO".

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
ronit
  • 31
  • 5
  • None of your original tags had anything to do with the question, so I have removed them. – Asad Saeeduddin May 17 '13 at 00:12
  • 1
    exact duplicate of [Method like String.contains() in JavaScript](http://stackoverflow.com/questions/1789945/method-like-string-contains-in-javascript) – Bergi May 17 '13 at 01:19
  • Notice that [jQuery's `:contains()` selector](http://api.jquery.com/contains-selector/) is for text nodes in the DOM, and has nothing to do with what you want here. Neither does the [`$.contains` method](http://api.jquery.com/jQuery.contains/). – Bergi May 17 '13 at 01:20

3 Answers3

4

This has nothing to do with jQuery selectors. Simply use the indexOf method to find whether the string stored in your variable contains the substring "CHICAGO":

if (yourString.indexOf("CHICAGO") !== -1) {
    //do something
}

If you want to check whether the string starts with the text "CHICAGO", you can use:

if (yourString.indexOf("CHICAGO") == 0) {
    //do something
}

You can read more about indexOf here.

EDIT: In your comments to Derek's answer, you mentioned that matching should be case insensitive (although this information isn't present in the question). If that is the case, simply use:

if (yourString.toUpperCase().indexOf("CHICAGO") == 0) {
    //do something
}
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
1

.contains() checks to see if an element is a direct descendent of another DOM element. It is not useful at all for what you are trying to do.

You should use .indexOf().

If you need your test to be case-sensitive:

if ([ITEM_NAME].indexOf('Chicago') === 0)

Otherwise, if it needs to be case-insensitive:

if ([ITEM_NAME].toUpperCase().indexOf('CHICAGO') === 0)
Derek Henderson
  • 9,388
  • 4
  • 42
  • 71
  • Hi Derek thanks,Does it satisfy for all itemname that starts with "chicago"? – ronit May 17 '13 at 00:18
  • Right now only 'CHICAGO'. Does case matter? I could make it match 'CHICAGO', 'chicago', 'Chicago', 'cHiCaGo', etc. Let me know. – Derek Henderson May 17 '13 at 00:20
  • Hi Derek thanks but i want that starts with "CHICAGO". It could be "CHICAGO NEW","CHICAGO OLD" ,"CHICAGO PURE VEG". – ronit May 17 '13 at 00:23
  • Ok, the first one does match all of those. My question is does it matter if it's all uppercase, because you wrote 'chicago' in your comment instead of 'CHICAGO'. I've now given you two examples, one will match anything that starts with 'CHICAGO', all uppercase, but not something that starts with 'chicago', lowercase, or that has 'CHICAGO' but not at the start. The second will match anything that starts with 'CHICAGO' or 'chicago' (or any combination of those letters upper- or lowercase) but only when 'Chicago' is at the start. – Derek Henderson May 17 '13 at 00:28
  • list always starts with "Chicago". – ronit May 17 '13 at 00:30
  • @ronit, you're not answering my question. You have now given me 3 versions of the name of the city in Illinois. We all understand that this word has to start the phrase, and both my answer and Asad's do that. But it is important to know whether it can only match 'CHICAGO' (all uppercase letters) or also match 'chicago' (all lowercase letters) and 'Chicago' (a mixture of uppercase & lowercase letters). This is important in determining which answer, mine or Asad's is best. They both do pretty much the same thing, but knowing precisely what you need will tell us which is the best answer. – Derek Henderson May 17 '13 at 00:37
  • Hi Derek its a mixture of both lower and uppercase letter.(Chicago).First character is upper then other all are lower. – ronit May 17 '13 at 00:45
  • In that case, Asad's answer will not work for you. As explained in my now edited answer, you have to use `.match()` because that uses a regular expression, allowing for case-insensitivity. If it will always use 'Chicago' use `.indexOf`, but if it could be any combination use `.match()`. Basically, `.indexOf` is a lot faster, but `.match()` is more powerful. – Derek Henderson May 17 '13 at 00:54
  • Hi Derek Thanks Alot. I have a list of 20-25 item names.ALL names starts with "Chicago Textile abc", Chicago Textile pure",Chicago Textile xyz" etc. so if ([ITEM_NAME].match(/^chicago/i)) will work??? – ronit May 17 '13 at 01:04
  • @ronit, as I've explained in my answer above, yes. **But**, it might be your best choice, and it might not be your best choice. If all your names start with 'Chicago', then `if ([ITEM_NAME].indexOf('Chicago') === 0)` is the better choice because it's a lot faster. However, if sometimes instead of 'Chicago' the name starts with 'CHICAGO' or 'chicago', then you have to use `if ([ITEM_NAME].match(/^chicago/i))`. – Derek Henderson May 17 '13 at 01:08
  • @ronit Do note that the `indexOf` approach still works with case insensitive matching. Simply use `yourString.toUpperCase().indexOf("CHICAGO")`. This was not mentioned in the question, so I did not address it in my answer. – Asad Saeeduddin May 23 '13 at 16:07
  • @Asad, yes, I hadn't considered changing the string's case, but that does work and it is significantly faster than match. I'll update my answer. – Derek Henderson May 23 '13 at 16:23
0

I would use a regex if (string.match(/chicago/i)) {...} it's case insensitive

Sagish
  • 1,065
  • 8
  • 13