Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 27-07-2005, 10:21 PM   #1 (permalink)
Guest
 
Posts: n/a
Default javascript for deleting repeating char


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
 
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 27-07-2005, 10:40 PM   #2 (permalink)
I'm back!
 
Join Date: May 2004
Location: Mumbai, Maharashtra
Posts: 65
Default ?

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
__________________
Little Knowledge is Dangerous but a lot of Ignorance is just as Bad!
Minimalistix is offline  
Old 28-07-2005, 02:48 PM   #3 (permalink)
Guest
 
Posts: n/a
Default

all repeating chars should be removed
 
Old 29-07-2005, 06:40 PM   #4 (permalink)
I'm back!
 
Join Date: May 2004
Location: Mumbai, Maharashtra
Posts: 65
Default 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: http://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 - http://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
__________________
Little Knowledge is Dangerous but a lot of Ignorance is just as Bad!
Minimalistix is offline  
Old 30-07-2005, 02:33 PM   #5 (permalink)
Guest
 
Posts: n/a
Default

thank you man i was just looking for such script.thanx..
 
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Krow
- by abhidev
- by topgear
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 07:01 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2