Im trying to get the position number of a certain character in a string for example I wanna get the number of the 2 in this string
var = "hello 2 world!";
Please help me to solve this Im stuck
Im trying to get the position number of a certain character in a string for example I wanna get the number of the 2 in this string
var = "hello 2 world!";
Please help me to solve this Im stuck
var test = 'Hello 2 world';
var pos = test.indexOf('2');
// pos === 6
The indexOf() method returns the index within the calling String object of the first occurrence of the specified value. Returns -1 if the value is not found.
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf for more detail
let test = "hello 2 world";
let array = test.split('');
for (let i = 0; i < array.length; i++) {
if (!isNaN(array[i])) {
return i;
}
}