Results 1 to 4 of 4

Thread: how to find a character in a variable

  1. #1
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,875
    Thanks
    49
    Thanked 263 Times in 255 Posts
    Blog Entries
    56

    Default

    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.

  2. #2
    Join Date
    Mar 2011
    Posts
    1,926
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

    Default

    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/...String/indexOf
    E.g.
    Code:
    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.

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,153
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default

    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. What I am/was looking for is the javascript equivalent of that.

    PHP Code:
    <?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'";
    }
    ?>
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,153
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default

    Got it. I replaced

    Code:
    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

    Code:
    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;
    }
    To choose the lesser of two evils is still to choose evil. My personal site

Similar Threads

  1. Replies: 7
    Last Post: 06-13-2011, 07:18 AM
  2. need help bolding a character
    By minidoggy in forum CSS
    Replies: 2
    Last Post: 02-28-2009, 09:42 PM
  3. special character!!!
    By erwinds in forum JavaScript
    Replies: 1
    Last Post: 09-25-2008, 06:13 PM
  4. javascript variable character limit??
    By LifeIsBeta in forum JavaScript
    Replies: 6
    Last Post: 04-25-2007, 12:18 PM
  5. there is no character data...
    By boxxertrumps in forum Other
    Replies: 6
    Last Post: 02-12-2007, 10:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •