This question already has answers here:
Remove a character at a certain position in a string - javascript [duplicate]
(8 answers)
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 18 days ago.
I'm trying to do a calculation with time and need to remove the ":" which splits hours and minutes.
My array currently holds a string value of "12:04"
I created a for loop to iterate through the second array string by length, check for a :, then remove that character and log the new output. However, my logic is not working as intended. If you can, please let me know what I did wrong so I can fix my issue.
for (let i = 0; i < content[2].length; i++) {
if (content[2].charAt(i) === ":"){
content[2].slice(i);
console.log(content[2])
}
}
If you are sure that ":" will appear only once, then keep it simple
content[2] = content[2].replace(":", "");
Full code:
const result = content.map(str => str.replace(":", ""))
I think this works well:
content.split(':').join('')
Here is the output I got from the console:
"12:04".split(':').join('')
'1204' // Output
Related
This question already has answers here:
How do I find an exact word in a string?
(5 answers)
Closed 28 days ago.
I want to compare the following two variables to see if they match exactly or not.
What I am doing right now is:
var string1 = "S1";
var string2 = "LS1 B26 M90";
let result = string2.indexOf(string1);
It returns 1 which means S1 exists in string2. I want it to look for "S1" and not to match with "LS1".
you can simply achive by below:
String("LS1 B26 M90").split(" ").includes("LS1")
String("LS1 B26 M90").split(" "): convert string into string list.
.includes("LS1"): will check the existence it return true in case of
match otherwise false.
This question already has answers here:
Javascript How to get first three characters of a string
(2 answers)
Keep only first n characters in a string?
(7 answers)
Closed 7 days ago.
I'm confused where I want to split the correspondence_code value like in the image below : let say I want to get just the first 5 digits like 16020 it is possible just getting the first 5 value? Thanks in advance
var result = objt['psgc'].filter(obj => {
return obj.geographic_level === "province"
})
// I want to split the value result -> correspondence_code with only first 5 digits
This question already has answers here:
Count the number of integers in a string
(6 answers)
Closed 4 years ago.
I currently have a text input in my HTML document and some buttons below it. When pressed, these will run a function input(some number). I have added a feature to the function that prevents the length of the string for the input from being above 5. However, sometimes the string (currentAnswer) will be set to something like -768.. When this happens, the function will not let you enter any more numbers, when instead you should be able to enter 2 more digits. So how would I find the length of just the numbers in a string? Here is my code:
function input(num) {
currentAnswer = document.getElementById("answerBox").value;
answerLength = currentAnswer.length;
if (answerLength < 5) {
document.getElementById("answerBox").value = currentAnswer + num;
answerLength = answerLength + 1;
}}
As you can see, I am using currentAnswer.length to get the length of the string. But I only want the amount of digits. Can someone please help me?
Count the number of integers in a string
alert("g66ghy7".replace(/[^0-9]/g,"").length);
:)
This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 5 years ago.
I need JavaScript algorithm that can match substring of a sting?
subStringFinder('abbcdabbbbbck', 'ab')
should return index 0
and
subStringFinder('abbcdabbbbbck', 'bck') should return index 10
Could you please tell me how to write this code?
--EDIT:
Thanks to #Jonathan.Brink I wrote that code and it did the trick:
function subStringFinder(str, subString) {
return str.indexOf(subString);
}
subStringFinder('abbcdabbbbbck', 'bck') // -> 10
You are looking for the indexOf function which is available via the built-in string type (as well as array).
Example:
var str = "abbcdabbbbbck";
var n = str.indexOf("bck");
// n is 9
Probably, rather than having a custom subStringFinder function it would be better to just use indexOf.
This question already has answers here:
Finding the nth occurrence of a character in a string in javascript
(6 answers)
Closed 9 years ago.
I have a JavaScript variable which contains something like "fld_34_46_name". I need to be able to find the location of the THIRD _. The numbers, and name are not always the same (so, the string might also look like "fld_545425_9075_different name_test").
Is this possible? How could I do it?
Use the indexOf method three times:
var i = s.indexOf('_');
i = s.indexOf('_', i + 1);
i = s.indexOf('_', i + 1);
Note: If the string might contain fewer than three underscores, you would need to check for -1 after each time.