Remove a substring from a string in Jquery [duplicate] - javascript

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

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/

Deleting the last part of a string if its longer than 14 letter [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 5 years ago.
I have a function which gives random Playernames which are sometimes longer than 14 letters. If that happens i want to delete the last characters so its 14 letters long and add "..." to it
Thats what I got so far. I tried substring too but it didnt work as well.
Thanks for helping
var playername;
function delString(){
if (playername.length >= 14){
var rest = playername.length - 14;
playername.slice(0, playername.length -rest);
playername = playername + "...";
}
}
I tried substring too but it didnt work as well
No it works, when you use substring() it will return a new string and doesn't affect the original one, so you need to assign this result in your old string.
This is how it should be implemented:
function delString(name){
if (name.length >= 14){
name = name.substring(0, 13);
name += "...";
}
return name;
}
Demo:
var playername = "I am longer than 14 chars";
function delString(name){
if (name.length >= 14){
name = name.substring(0, 13);
name += "...";
}
return name;
}
console.log(delString(playername));

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

Extract sub string from last in a string JS [duplicate]

This question already has answers here:
endsWith in JavaScript
(30 answers)
Closed 9 years ago.
I need to write JS function which returns true if string contains - depreciated in its last otherwise false.
For example:
var somestring = "string value - depreciated";
function should return true in above example.
function isDepreciated(var S)
{
//Need to check for substring in last
//return true or false
}
One possible solution is to use search function but that means that if - depreciated comes within string then it will also return true. I really need to find weather substring is in last or not.
Please help.
Add the below code in your JS
function isDepreciated(string){
return /(-depreciated)$/.test(string);
}
You'll want to use the Javascript string method .substr() combined with the .length property.
function isDepreciated(var id)
{
var id = "string value - depreciated";
var lastdepreciated = id.substr(id.length - 13); // => "- depreciated"
//return true or false check for true or flase
}
This gets the characters starting at id.length - 13 and, since the second argument for .substr() is omitted, continues to the end of the string.
function isDepreciated(S) {
var suffix = "- depreciated";
return S.indexOf(suffix, S.length - suffix.length) !== -1;
}
You could use currying: http://ejohn.org/blog/partial-functions-in-javascript/
Function.prototype.curry = function() {
var fn = this, args = Array.prototype.slice.call(arguments);
return function() {
return fn.apply(this, args.concat(
Array.prototype.slice.call(arguments)));
};
};
With the helper curry function you could create your isDepricated check:
String.prototype.isDepricated = String.prototype.match.curry(/- depreciated$/);
"string value - depreciated".isDepricated();
Or use .bind():
var isDepricated = RegExp.prototype.test.bind(/- depreciated$/);
isDepricated("string value - depreciated");
function isDepreciated(S){
return (new RegExp(" - depriciated$").test(S));
}
how about just use regular expression
var myRe=/depreciated$/;
var myval = "string value - depreciated";
if (myRe.exec(myval)) {
alert ('found');
}
else{
alert('not found');
}
lots of answers are already here (the one with $ is preferred), even though i also had to write one, so it will also do your job,
var somestring = "string value - depreciated";
var pattern="- depreciated";
function isDepreciated(var s)
{
b=s.substring(s.length-pattern.length,s.length)==pattern;
}
function isDeprecated(str) {
return ((str.indexOf("- depreciated") == str.length - "- depreciated".length) ? true : false);
}
isDeprecated("this")
false
isDeprecated("this - depreciated")
true
isDeprecated("this - depreciated abc")
false
Ok, I haven't run this code on a browser, but this should give a basic idea of what to do. You might have to tweak some of the conditions if needed.
var search = "- depricated";
var pos = str.indexOf(search);
if(pos > 0 && pos + search.length == str.length){
return true;
}
else{
return false;
}
Edit: indexOf() returns the start index of the string.

check variable if char exist or not [duplicate]

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

Categories