Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts

    how do you replace a char in a string

    Hi

    How do you replace a char in a string?

    eg var d = f.replace(char(160), "-");


    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Something like this:-

    Code:
    txt = txt.replace(//g,"-");
    txt = txt.replace(//g,"-");
    txt = txt.replace(//g,"-");
    txt = txt.replace(//g,"-");
    Or perhaps you mean:-

    Code:
    <script type = "text/javascript">
    
    var tstr = "Hello ltere World";
    var bstr = '';
    
    for (i=0; i<tstr.length; i++) {
    if (tstr.charCodeAt(i)>127) {  // outside ASCII range
    bstr += '-';  // change to a hyphen
    } 
    else {
    bstr += tstr.charAt(i);
    }
    }
    alert (bstr);
    
    
    </script>
    Quizmaster: In legend, the gryphon has the body of a lion and the head and wings of which bird?
    Contestant: A goose
    Last edited by Philip M; 05-29-2012 at 07:57 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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