Hi Nik! Here's the script.
Hi Nik,
Sorry! I couldn't post the Script yesterday. I had to go somewhere.
But, here it is now!!
I've even uploaded the Script online at my website.
You can check out the Demo at: *byrequest.ravi.co.in/nik_for_you.html (IE Only)
Just right-click, select view-source and search for the text "function filter ()" to see the the code.
Plz Note:
Although the Demo only works in IE, the source code provided below works on all browsers.
I wrote the Script with two flavours.
One exactly the way you asked me to i.e remove ALL the characters that appear more than once and
the second one where the original character i.e. its first occurence is preserved but all others are removed.
The second flavour can handle strings like "aaabbbcc" to give "abc". Using the first one, you'll get a null "" string.
If you find the above code, which checks the radio buttons to decide on the flavour,
difficult to adapt to your needs then simply look at the following simplified version of the code.
Please ignore the comments. Lines following "//" are comments and You can safely remove them along with the "//".
Code:
<script type="text/javascript" language="javascript">
<!--
// A simple JavaScript to remove duplicate characters from a String.
// 9:12:56 PM 7/28/2005 - by Ravi - *Ravi.co.in
function filter (str, pre) {
var ctr ;
var chr ;
// alert (str) ;
var tmp = str.split (" ") ;
str = tmp.join ("") ;
// alert (str) ;
var len = str.length ;
// alert (len) ;
for (ctr = 0 ; ctr < len ; ctr++) {
chr = str.charAt (ctr) ;
if (str.lastIndexOf (chr) != ctr) {
// alert (chr) ;
tmp = str.split (chr) ;
str = tmp.join ("") ;
// str = str.replace (chr, "") ;
// alert (str) ;
if (typeof (pre) == "undefined" || pre == 0) {
ctr-- ;
} else {
// alert (str + ", " + ctr + ", " + chr + ", " + str.length) ;
str = str.substring (0, ctr) + chr + str.substring (ctr, str.length) ;
// alert (str) ;
}
len = str.length ;
}
}
return (str) ;
}
//-->
</script>
Parameters:
The above function expects two parameters. The "1st one is the string to filter" and
the "2nd one is 0 to filter exactly as you want and 1 to filter with first occurences preserved".
Examples:
Result = filter ("anik aish", 0) ; makes Result = "nksh"
Result = filter ("anik aish", 1) ; makes Result = "aniksh"
Plz Note:
The 2nd parameter is optional and if not provided the function would filter exactly the way you wanted.
Example: Result = filter ("nik aish") ; makes Result = "nkash".
That's all nik!!
In case you still have some problems feel free to contact.
Ravi