Replace Chars in String Problem

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jun 2006
Posts: 61
Reputation: dev.cplusplus is an unknown quantity at this point 
Solved Threads: 0
dev.cplusplus's Avatar
dev.cplusplus dev.cplusplus is offline Offline
Junior Poster in Training

Replace Chars in String Problem

 
0
  #1
Aug 28th, 2008
Hi to all, hi have the following problem, I hope someone can help me.
I need to replace special chars that appear in a string.

for example if I have the string:
this- is a !string with special chars-
I want to receive
this45 is a 33string with special chars45

* The numbers in the string are the ascii code of every char.

I try using the "replace" method, but only work with the first char appearance of the char, if I the char appear more that once in the string
the replace method replace only the first appearance.

for example if I have the string:
this- is a !string with special chars-
I receive:
this45 is a 33string with special chars-

Leaving the second '-' without changing it.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Replace Chars in String Problem

 
0
  #2
Aug 28th, 2008
Use the replace function iteratively as long as given character occurs in the target string (can be found out using indexOf ). Post your code so that we can suggest possible modifications.


Another complicated, but elegant solution would be to use the replace function which uses a Regular Expression along with the g(global) modifier, substitution variable and anonymous functions. Something like:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // 'b' here contains the matched character.
  2. "!AB#CD%EF@".replace
  3. (/([!#%$@])/g,
  4. function(a, b) {
  5. return(b.charCodeAt(0));
  6. });
Last edited by ~s.o.s~; Aug 28th, 2008 at 2:09 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Replace Chars in String Problem

 
0
  #3
Aug 29th, 2008
If you're doing this for the purpose of input sanitizing, there are probably less 'safe' characters than there are 'unsafe' characters, create a new empty string ( string 2 ) loop over the input string ( string 1 ), one character at a time, if the character is in the range a-b, A-B, 0-9, then place it at the end of string 2, otherwise place the code of the character at the end of string 2. Simple as anything.

Don't bother trying to replace every special character ( especially if your sanitizing for a database or a shell script ) because you'll likely miss one, if you only allow the safe characters, the only risk is that you forget to allow something safe, and that's quite a bit better than forgetting to block something unsafe. Basically, it's always safer to whitelist than it is to blacklist.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Replace Chars in String Problem

 
0
  #4
Aug 29th, 2008
> If you're doing this for the purpose of input sanitizing

That doesn't seem to be the case here since converting special / blacklisted characters to their equivalent codes doesn't serve any purpose. Also, encodeURIComponent(string) seems to be a more appropriate escaping function than manually doing so.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Replace Chars in String Problem

 
0
  #5
Aug 29th, 2008
Regardless of whether you're doing this for input sanitising or not, the method I suggested has the best complexity guarantee.. it's always O( n ). Worst case, assuming a string of all special characters, for calling replace multiple times is O( 1 + 2 + 3 + 4 + .. + n ), and I have no idea about the worst case for regexes; but I assume it's not as good as O( n ).. [ Even it it is O( n ), the implicit factor on n and the initial overhead is likely higher than it is for manually doing it by looping, although, if the input strings are epicly massive, maybe the overhead of regex setup is outweighed ]. Of course, if you blacklist the special characters, you have to multiply all those n's by a factor k ( number of illegal characters ), if you whitelist only alphanumeric characters, you can check for the character being in the range a-z,A-Z,0-9 using charCodeAt(i) and the known ascii values for the starts and end of those ranges == a small constant factor.

Looping and checking each char is also the simplest one to write/understand, so it's a win all round IMHO.

Originally Posted by ~s.o.s~
That doesn't seem to be the case here since converting special / blacklisted characters to their equivalent codes doesn't serve any purpose.
I disagree, this could certainly be used as an effective one-way sanitization technique -- coupled with wrapping the processed input in a pair of quotes -- but of course, who am I to second guess the OP.

Originally Posted by ~s.o.s~
Also, encodeURIComponent(string) seems to be a more appropriate escaping function than manually doing so.
Probably. Certainly if you ever want to unescape.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Replace Chars in String Problem

 
0
  #6
Aug 29th, 2008
> I disagree, this could certainly be used as an effective one-way sanitization technique

Sanitization by converting special characters to their numeric equivalents rather than escaping them? When talking about web applications, the majority of escaping which happens is to prevent arbitrary input from messing up manual form submissions / asynchronous submits for which encodeURIComponent works nice enough. So I still don't see any real need to roll something custom unless of course the exercise is for learning purpose.

> Probably. Certainly if you ever want to unescape.

At least in J2EE, the unescaping is automatic so

And as far as the complexity analysis is concerned, one should rather use tried and tested library / proven methods than worrying oneself with premature optimization. Don't optimize unless you have benchmarked and it really is slow in the context of your requirements / application design. And BTW, regular expression engines nowadays are super optimized state machines which can almost come close to the manual search and replace when used with care. :-)
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Replace Chars in String Problem

 
0
  #7
Aug 29th, 2008
And as far as the complexity analysis is concerned, one should rather use tried and tested library / proven methods than worrying oneself with premature optimization.
I absolutely agree, unless the task at hand is simple enough to be considered a "primitive operation" in itself, and personally, I consider looping over a string's characters to be something thats -- in all practical purposes -- irreducible, without either losing generality [ i.e. replace wont work with ranges ], or requiring a complex framework [ regex support ranges just fine, but you can't deny they could be considered overkill in many contexts ]. Regex are fast, but they have to be pre-processed ( initial overhead ), and the abstraction of regexes incurs a cost ( unless the implementation is really, really excellent )

I wasnt strictly refering to sanitising as in sanitising a post/request value, I was considering sanitising a string for a database or a shell script, where input corruption is much more acceptable than integrity corruption.. but I pretty much forgot this is the Javascript forum, and any sanitization of that type should ( must! ) be done at the server anyway... and thats not gonna be done in Javascript.. unless using old-school ASP+serverside JS.. >_>
Last edited by MattEvans; Aug 29th, 2008 at 5:29 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Replace Chars in String Problem

 
0
  #8
Aug 30th, 2008
> but I pretty much forgot this is the Javascript forum

Bah, I rest my case. :-P
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: dev.cplusplus is an unknown quantity at this point 
Solved Threads: 0
dev.cplusplus's Avatar
dev.cplusplus dev.cplusplus is offline Offline
Junior Poster in Training

Re: Replace Chars in String Problem

 
0
  #9
Sep 3rd, 2008
Sorry to answer only now,but I was very busy...
Thanks for all the replies, this is the code I'm using:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var myString = "MyStr-ing-";
  2. var specialChars = "!@#$%^&*()-+";
  3. for(var nIndex=0; nIndex<specialChars.length; nIndex++)
  4. myString=myString.replace(specialChars.charAt(nIndex),specialChars.charCodeAt(nIndex);

The original code is "MyStr-ing-" and after running the code the string will be "MyStr45ing-", replacing only the first appearance, any ideas how to do it in a better way, and I way that will change all the chars.
Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Replace Chars in String Problem

 
0
  #10
Sep 3rd, 2008
Something like this maybe:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!--
  2. Replace special characters in a string.
  3. Copyright (C) 2008 sos aka Sanjay
  4.  
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. -->
  18. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  19. "http://www.w3.org/TR/html4/strict.dtd">
  20. <html>
  21. <head>
  22. <meta http-equiv="Script-Content-Type" content="text/javascript">
  23. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  24. <title>Example</title>
  25. <script type="text/javascript">
  26. // Error checking omitted for brevity
  27. function doIt() {
  28. var special = "@#$%^&";
  29. var str = "@Hello @How@ $Do %You $Do?";
  30. var strArr = str.split(''); // Convert string to an array
  31. for(var i = 0, maxI = strArr.length; i < maxI; ++i) {
  32. var ch = strArr[i];
  33. // If the current character is one among the special characters,
  34. // replace it with its character code.
  35. if(special.indexOf(ch) > -1) {
  36. strArr[i] = str.charCodeAt(i);
  37. }
  38. }
  39. var modifiedStr = strArr.join('');
  40. alert("Orig String: " + str + "\nModified String: " + modifiedStr);
  41. }
  42. doIt();
  43. </script>
  44. </head>
  45. <body id="bdy">
  46. <p>A test page</p>
  47. </body>
  48. </html>
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC