View Full Version : Resolved how to find a character in a variable
molendijk
10-28-2019, 06:56 PM
Hi James,
I'm not sure I understand what you are tryring to do, but finding a character in a string can be easily done with the help of the substring() method.
keyboard
10-28-2019, 11:23 PM
Hey James,
Just to add the easiest way to find if a character is present in a string is String.indexOf(); https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
E.g.
let exampleString = "This is a string with a backslash \\";
//This evaluates to true as there is a backslash.
if(exampleString.indexOf("\\") > -1)
{
}
For your specific question I'm also not sure what you're asking exactly.
james438
10-29-2019, 12:43 AM
Thank you. I will try out your suggestions.
I think that both of your examples have pointed me in the right direction. If it works I will post the solution. The script I am working on updating is a simple gallery script with a few buttons that allows my to cycle through images.
Here is one of the examples listed on the PHP page for strpos (https://www.php.net/manual/en/function.strpos.php). What I am/was looking for is the javascript equivalent of that.
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// The !== operator can also be used. Using != would not work as expected
// because the position of 'a' is 0. The statement (0 != false) evaluates
// to false.
if ($pos !== false) {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
} else {
echo "The string '$findme' was not found in the string '$mystring'";
}
?>
james438
10-29-2019, 01:10 AM
Got it. I replaced
function newio(n){
newio.Num += n || -newio.Num;
newio.Num = newio.Num < 0? newio.count - 1 : newio.Num % newio.count;
newio.imgo.src = '/images/screenshots/' + newio.ar[newio.Num];
newio.output.value = newio.ar[newio.Num];
return false;
}
with
function newio(n){
newio.Num += n || -newio.Num;
newio.Num = newio.Num < 0? newio.count - 1 : newio.Num % newio.count;
var n=newio.ar[newio.Num];
var n = n.search("/");
if (n<0)
{
newio.imgo.src = '/images/screenshots/' + newio.ar[newio.Num];
}
else
{
newio.imgo.src=newio.ar[newio.Num];
}
newio.output.value = newio.ar[newio.Num];
return false;
}
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.