Click to See Complete Forum and Search --> : Replacing characters


enigma
02-07-2003, 03:01 AM
I see that khalidali got angry at me because I was acting like if i was his boss . I'm sorry OK ? Well whatever . the script i found is on
http://javascript.internet.com/forms/replace-characters.html
and i don't know how to make it replace other charcters it just replaces "a" with "z" I would like that it would replace "a" with "y" , NO PROBLEM i just replace the "z" in the
out = "a"; // replace this
add = "z"; // with this
with "y" but how do I make it also replace other characters like "d" with "t" . I added another
out = "a"; // replace this
add = "z"; // with this
and it looked so
out = "a"; // replace this
add = "y"; // with this
out = "y"; // replace this
add = "o"; // with this
but now it only replaces y :confused: . I want that replces more then 1 character i want that every character will be replaced . Like this a with y , y with o , o with something else and so on . Every character schould have an other character that will replace it after you press the button . And could you please make a second text box so that after you press the button the replaced text will not appear in the same text box but in the second text box . OK and i'm sorry khalidali63 OK . But if you are still angry at me tthere will be no more "special thanks to khalidali63" if you are not khalidali then it's you're lucky day . UNDER THE SCRIPT I WILL PUT AN "SPECIAL THAKS TO SECTION AND YOU'RE NAME IS GOING TO BE THERE IF YOU WILL HELP . And for those who think i'm acting as if i were you're boss listen i'm stupid i'm a worm . If you're going to help me if i act like an idiot then i have no other choise but to ....... sing "I'm a little teapot .... . Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please Please
Help me

vickers_bits
02-07-2003, 04:10 AM
i don't know what code you're using at the moment but maybe thi will help:

function findReplace(s,f,r){
//Replaces [f] with [r] within [s];
//eg: findReplace("helloworld","o","") returns "hellwrld";
var iMatch, sub1, sub2;
iMatch=0;
while(iMatch>=0){
iMatch=s.indexOf(f);
if(iMatch<0) return s;
sub1=s.substring(0,iMatch);
sub2=s.substring(iMatch+f.length,s.length);
s=sub1+r+sub2;
}
return(0);//NN4;
};

Charles
02-07-2003, 05:09 AM
<script type="text/javascript">
<!--
String.prototype.translate = function (c1, c2) {
var a1 = c1.split('');
var a2 = c2.split('');
var a = this.split('');
for (i=0; i<a.length; i++) {for (j=0; j<a1.length; j++) {if (a[i] == a1[j]) a[i] = a2[j]}}
return a.join('');
}

alert ('hello world'.translate('aeiou', 'AEIOU'));
// -->
</script>

Charles
02-07-2003, 06:18 AM
Oops, there's a wee bit of a problem with that one. Mayhaps this one will do:

<script type="text/javascript">
<!--
String.prototype.translate = function (s1, s2) {
var a1 = s1.split('');
var a2 = s2.split('');
var o = new Object();
for (i=0; i<a1.length; i++) {o[a1[i]] = a2[i]}
var a = this.split('');
for (i=0; i<a.length; i++) {if (o[a[i]]) a[i] = o[a[i]]};
return a.join('');
}

alert ('hello world'.translate('abcdefghijklmnopqrstuvwxyz', 'bcdefghijklmnopqrstuvwxyza'));
// -->
</script>

khalidali63
02-07-2003, 07:14 AM
Originally posted by enigma

OK and i'm sorry khalidali63 OK . But if you are still angry at me tthere will be no more "special thanks to khalidali63" .

Thats all most of us really care that we have get "Thank yoo" so I will not miss a chance like that..:-)

Your question is not complete in the sense that you do no ******** which character should be replaced by which.
now I am writing this function,This is how it will work.In your previous post it was'nt clear that you want a random character be replaced or there is a specific list of characters that would replace any alphbetical characers.

Now here is the alphabet.Please respond that which character you want ot be replaced by what characers.

"abcdefghijklmnopqrstuvwxyz"

Saying sorry is corageous thing to do.And it actually shows some ones moral beliefs unlike some others in here who only believe to maliciously hurt others.
Anywasy respond to this I'll make sure your concerns are dealt with ,and I'dlove to see a "Special Thanks to Khalid" section

:D

cheers

Khalid

Charles
02-07-2003, 07:25 AM
Or you could simply use the second of my translate methods posted above. You will notice that the method takes as parameters two expressions that are evaluated as strings. It returns the String with any characters in the first expression replaced with characters in the second expression. It's not as robust as Perl's transliteration operator but it works much the same way. (Actually, I've tried to give you an object oriented version of XPATH's translate function. See http://saxon.sourceforge.net/saxon6.5.2/expressions.html#StringExpressions.)