3

var qtr = $('div').text();
var qtr1 = qtr.split(',')
console.log(qtr1.length)

for (var i = 0; i < qtr1.length; i++) {
  console.log(qtr1[i]);
}
console.log(qtr1);
if ($.inArray('1st qtr', qtr1)) {
  alert('1st');
}
if ($.inArray('2nd qtr', qtr1)) {
  alert('2nd');
}
if ($.inArray('3rd qtr', qtr1)) {
  alert('3rd');
}
if ($.inArray('4th qtr', qtr1)) {
  alert('4th');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>1st qtr,2nd qtr,3rd qtr,4th qtr,</div>

I have a div with text in it i want to check if a specific text is in the div. I tried splitting the div text and getting checking $.inArray but the first text is not being shown even if it is missing.

Problem:

  1. Why do i get 5 as length in even if there are only 4 text. An extra space is being added at the last
  2. Why is the first text which is 1st qtr not shown even if it is existing.
  3. What is the best to check if a text is existing in a text.
Tushar
  • 85,780
  • 21
  • 159
  • 179
guradio
  • 15,524
  • 4
  • 36
  • 57

1 Answers1

5

Why do i get 5 as length in even if there are only 4 text. An extra space is being added at the last

The string contains , at the end. When the string is split using ,, an empty string is added as the last element since there is nothing after the last ,. You can remove the last , by using regex.

Why is the first text which is 1st qtr now shown even if it is existing.

The inArray returns the index of the sub-string found in the string. So, for the first string the index 0 is returned. As 0 is falsy value, the if condition is evaluate to false and the block is not executed.

Updated Code

var qtr = $('div').text().replace(/,$/, '');
var qtr1 = qtr.split(',');

console.log(qtr1.length)

if ($.inArray('1st qtr', qtr1) !== -1) {
  alert('1st');
}
if ($.inArray('2nd qtr', qtr1) !== -1) {
  alert('2nd');
}
if ($.inArray('3rd qtr', qtr1) !== -1) {
  alert('3rd');
}
if ($.inArray('4th qtr', qtr1) !== -1) {
  alert('4th');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>1st qtr,2nd qtr,3rd qtr,4th qtr,</div>

What is the best to check if a text is existing in a text.

indexOf is the best to check if the string contains a substring

var str = '1st qtr,2nd qtr,3rd qtr,4th qtr,';

var contains = str.indexOf('4th qtr') > -1;

alert('str contains  4th qtr? ' + contains);
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • why add -1 at the condition? also 4th is not shown even if it is present i need to show all text since all are present – guradio Oct 06 '15 at 06:08
  • what you did is change the last `,` to `space` and did same thing except added `!== -1` in the condition?i want to understand what you did please bear with me. – guradio Oct 06 '15 at 06:13
  • @Pekka **1.** Removed the last comma from the string. **2.** Split the string by `,` **3.** The inArray returns the index of the sub-string found in the string. So, for the first string the index 0 is returned. As 0 is falsy value, the if condition is evaluate to false and the block is not executed. – Tushar Oct 06 '15 at 06:14
  • i see last question this one i really didn't understand id the index is returned as `0` why is it adding `!== -1` will get the first index? – guradio Oct 06 '15 at 06:16
  • give me 3 minutes to tick it as answer.please explain last question thank you – guradio Oct 06 '15 at 06:16
  • 1
    @Pekka The index of the first character is zero, just like array and when the element is not found in the array `-1` is returned, I hope this will clear the confusion. – Tushar Oct 06 '15 at 06:18
  • 1
    @Pekka For reference, http://stackoverflow.com/questions/18867599/jquery-inarray-how-to-use-it-right – Tushar Oct 06 '15 at 06:23