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.
4
  • if you want to highlight keywords you can use github.com/julmot/jmHighlight Commented Oct 6, 2015 at 6:06
  • @julmot i dont want to hightlight text i will use it as parameter for other purposes. Commented Oct 6, 2015 at 6:07
  • 2
    you have a comma at the end of text. that's the reason length is 5 Commented Oct 6, 2015 at 6:08
  • 2
    You get 5 as length, because the content ends in a comma - the empty string aftert that is counted as fifth element. Commented Oct 6, 2015 at 6:08

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

7 Comments

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
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.
@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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.