javascript for deleting repeating char

Status
Not open for further replies.
G

Guest

Guest
hi can anyone give me javascript to delete repeating duplicate char from string . and to make one string from than

eg

nik and aish . i is comman so final string will be nkash

thanx
 

Minimalistix

I'm back!
?

Hi nik,

Do u want ths to happen wth *only* th first char tht repeats itself, like "anik aish" to be "nikish" wth only "a" removed,
or, do you want all th repeating characters to be removed, like "anik aish" becomes "nksh" wth both "a" and "i" removed.

Ravi
 

Minimalistix

I'm back!
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
 
Status
Not open for further replies.
Top Bottom