In PHP, we use strpos to check if a string contains a substring.
strpos function— Find the position of the first occurrence of a substring in a string, it may return 0 as it's index value.
If you do a
==
compare that will not work, you will need to do a ===.
A
==
sign is a comparison and tests whether the variable
/ expression / constant to the left has the same value as the variable /
expression / constant to the right. A
===
sign is a comparison to see whether two variables / expressions / constants are equal AND
have the same type - i.e. both are strings or both are integers.Example:
1) to check $contract string contains "Sessional" substring in PHP
if(strpos($contract, 'Sessional') ! = = false)
2) to check $contract string does not contain "Sessional" substring in PHP
if(strpos($contract, 'Sessional') = = = false)
In JavaScript, we use indexOf function
The indexOf() method returns the position of the first occurrence of a specified value in a string.
This method returns -1 if the value to search for never occurs.
Example
1) to check contract string contains "Sessional" substring in JS
if( contract.indexOf("Sessional")!=-1)
2) to check contract string does not contain "Sessional" substring in JS
if( contract.indexOf("Sessional") = = -1)
More about string operation in Javascript
In JavaScript, To get the the first character of string contract, we use
contract.charAt(0)
To get a substring of a string for example
term1=(term).substring(0,4);
where 0 is start position, and 4 is end position, but not included.
To trim a string for example term
term.trim();
or using jQuery
$.trim(term)
Similarly in PHP we have substr function,
for example
echo substr('abcdefg', 0, 4);
return abcd, also not including end position.
No comments:
Post a Comment