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 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts

    replace certain part of string

    Hi, I can't find a javascript function to do this. Ideally I need the opposite of "charAt" to set the char at a specific position.

    I've looked on google a lot but can only find examples of string replacing (at no specific position).

    (also anyone know a downloadable javascript function reference?)

    Cheers.
    Daniel
    XZIST.org

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    You may find that the substring method offers you a way to achieve your goal.

    http://www.w3schools.com/jsref/jsref_substring.asp
    http://www.quirksmode.org/js/strings.html


    If we knew exactly how the script was intended to be implemented, then we could possibly put together a custom script for you.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may also use RegExp
    http://lawrence.ecorp.net/inet/samples/regexp-intro.php

    Give us a small example of what you need, and, yes, we migh show u an example
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts
    so there's no simple way?

    here's what I have at the moment
    Code:
    		var newstr = "";
    		
    		
    		for (x = 0; x < so.value.length; x ++)
    		{
    			if (x >= index && x < index+7)
    			{
    				newstr += colour.charAt(x-index);
    			}
    			else {
    				newstr += so.value.charAt(x);
    			}
    		}
    		
    		so.value = newstr;
    it replaces a colour in string "so.value" at position "index" with a different one "colour" (7 char hex code).

    At the moment i'm looping through the whole string which takes a significant few seconds. (as there's several long strings)
    Daniel
    XZIST.org

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    You could use string.replace using a regular expression

    substr would probably be a lot faster than looping

    But, I tried to figure it out, and it seems you can only read from random indeces, but not write to random indecex in a string.

  • #6
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I see
    Code:
    so.value = so.value.substring(0, index) + colour + so.value.substring(index+7, so.value.length);
    Thanks it's blazingly fast now.
    Daniel
    XZIST.org


  •  

    Posting Permissions

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