check variable if char exist or not [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to tell if a string contains a certain character in javascript?
Suppose I have a string in variable ex. var name="Stackoverflow". I want to check in this string if 'z' is exist or not? How can I check this? I don't want to find index or anything else I just want to check if value z is exist or not.
Suppose with code. I have a variable.
var deleteboxvalue = "1111111111111111111111";
if(!deleteboxvalue.indexOf('z') >= 0){
alert("0 not exist");
return false;
}

You can use indexOf like this:
var name = "Stackoverflow"
var charExists = (name.indexOf('z') >= 0) ? true : false;
alert(charExists);
Or just (as pointed out by #Felix Kling):
var charExists = (name.indexOf('z') >= 0);

Related

I'm trying to write a function that gets a string from a field in html and return it if the first and the last characters are equal [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
How to get first character of string?
(22 answers)
How can I get last characters of a string
(25 answers)
Closed 3 years ago.
I'm ought to do it without loops. with "if" and "else".
The user writes a random word in the text field at the html and the function needs to check if the first and last letters are equals.
If they are equal - the function will return the string without the first and the last characters.
if the characters are not equal - the function will return the written string by the user.
unction firstLastletter(){
let k = document.getElementsByClassName('text').value;
let other = 'changed the string';
if(k.slice(0) == k.slice(-1)){
return console.log(k);
}
else {
return console.log(other);
}
}
i know that i miss a lot.
but i have no clue how to implement the code in a right way.
many thanks..
Something like this?
function firstlastletter(s) {
var first = s.charAt(0);
var last = s.charAt(s.length-1);
if(first == last) return s.substring(1, s.length-1)
else return s;
}
https://jsfiddle.net/mte97hg1/19/

How to check value in javascript variable for include number or not? [duplicate]

This question already has answers here:
Check whether an input string contains a number in javascript
(15 answers)
Closed 7 years ago.
How to check value in javascript variable for include number or not ?
eg: var test = "abcd1234";
this var test include lower case and number.
How to use javascript to check var value include number or not ?
I tried to use isNaN function
var test = "abcd1234";
var test = isNaN(test);
if(test === true)
{ alert("not include number");
else
{ alert("include number");
But this code alert not include number because isNaN will check all data of var. but i want to check only a part of var. How can i do that ?
You must use regex instead.
var test = /\d/.test("abcd1234");
if (test === true) {
alert("include number");
} else {
alert("not include number");
}

Bit Pattern in JavaScript [duplicate]

This question already has answers here:
String Conversion in Javascript (Decimal to Binary)
(5 answers)
Closed 7 years ago.
I would like to ask help whether am I doing the right thing or not. You see I am trying to test myself by displaying the bit pattern of a number in the most efficient way as possible. But I'm having trouble on how to display the pattern cause I'm still learning javascript. Here's my code.
<script>
var bitPattern = function(given) {
for(var i = 1 << 31; i > 0; i = i / 2){
document.write((given & i) ? 1 : 0);
}
};
var number = prompt("Enter a number to convert: ");
bitPattern(number);
</script>
The best way to do this is:
var number = prompt("Enter a number to convert: ");
var bitPattern = parseInt(number).toString(2);
document.write(bitPattern);

Remove a substring from a string in Jquery [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I am writing a jQuery function on the click event of a checkbox list, which stores the value in a string when one item is checked and it removes the string when it is unchecked. I am taking the closed label text and insert and remove it from the string. I can add the strings but I am not able to remove it.
Here is my code:
var currentage = '';
$('#<%=chk_ange.ClientID%> input:checkbox').click(function () {
var str = $(this).next('label').text();
if ($(this).is(':checked')) {
if (currentage.indexOf($(this).next('label').text()) == -1) {
currentage = currentage + str;
}
alert('checked' + currentage);
}
else {
currentage.replace(str, "Hello");
//currentage.replace(str, 'None');
alert('unchecked' + currentage);
}
}
I am storing the values in a global variable so that i can compare the value in each click.
You have to assign the result of replace back to your variable like:
currentage = currentage.replace(str, "Hello");
See: String.prototype.replace() - MDN
You need to equal the result from currentage.replace(str, "Hello"); to currentage. String are immutable so the replace()function returns a new modified string.
currentage = currentage.replace(str, "Hello");

javascript if statement if url contains substring [duplicate]

This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 9 years ago.
So basically I want to do something different for a site if the url contains a specific string, lets say: 'foo'
So this is what I have:
var url = document.URL;
var substring = 'foo';
if(the substring is in the url){
//do something
}
else{
//do something
};
So what would go in the if() to make this possible?
You can use the indexOf method
// Function is used to determine whether a string contains another string
function contains(search, find) {
///<summary>Sees if a string contains another string</summary>
///<param type="string" name="search">The string to search in</param>
///<param type="string" name="find">The string to find</param>
///<returns type="bool">A boolean value indicating whether the search string contained the find string</returns>
return search.indexOf(find) !== -1;
}
Here's some sample usage:
var url = document.URL;
var substring = 'foo';
if (contains(url.toLowerCase(), substring.toLowerCase()) {
// Contains string
}
The contains function is case sentitive, however; as demonstrated in my example, you can make it incasesensitive by calling the StringPrototype.toLowerCase Method
You may use indexOf for example :
if (url.indexOf(substring)>=0) {
This is a forward-looking answer, and won't work in current implementations.
ECMAScript 6 is currently defining a String.prototype.contains method. This will allow you to do:
if (url.contains(substring)) {
Again, this is a future addition. Currently ECMAScript 6 (Harmony) is being drafted, and this could technically be removed, though it doesn't seem likely.
Current draft:
15.5.4.24 String.prototype.contains (searchString, position = 0 )
The contains method takes two arguments, searchString and position, and performs the following steps:
Let O be CheckObjectCoercible(this value).
Let S be ToString(O).
ReturnIfAbrupt(S).
Let searchStr be ToString(searchString).
ReturnIfAbrupt(searchStr).
Let pos be ToInteger(position). (If position is undefined, this step produces the value 0).
ReturnIfAbrupt(pos).
Let len be the number of elements in S.
Let start be min(max(pos, 0), len).
Let searchLen be the number of characters in searchStr.
If there exists any integer k not smaller than start such that k + searchLen is not greater than len, and for all nonnegative integers j less than searchLen, the character at position k+j of S is the same as the character at position j of searchStr, return true; but if there is no such integer k, return false.

Categories