Re: [Flashcoders] String Empowerment
Hi Steven, Maybe i'm saying something stupid here, but would this be a prettier way to do the capitalize? String.prototype.capitalize = function() { return this.charAt(0).toUpperCase() + this.substr(1).toLowerCase(); } gr, heerko On 28-jul-2006, at 0:39, Steven Sacks | BLITZ wrote: My follow up to the Array extensions is this set of String extension methods, again, borrowed from other languages. Time for optimization! Some methods are still in development - some pretty tricky checking. I also need to make _parseParams a bit more robust in allowing multiple arguments to be passed with the intersections of those strings being the result. -- // XString // Returns a copy of this with the first character converted to uppercase and the remainder to lowercase. String.prototype.capitalize = function() { var a = this.length; if (a == 0) return ; var c; var s = ; while (--a -(-1)) { c = this.charCodeAt(a); if ((c 64 c 91 a 0) || (c 96 c 123 a == 0)) c = c ^ 32; s = String.fromCharCode(c) + s; } return s; }; // If n is greater than the length of str, returns a new String of // length n with str centered between r; otherwise, returns str. // If r is not passed, r is a space String.prototype.center = function(n, r) { var a = this.length; if (n a) { if (r.length == 0) r = ; var d = n - a; var cl = Math.floor(d / 2); sl = ; while (--cl -(-1)) { sl += r; } var cr = Math.ceil(d / 2); sr = ; while (--cr -(-1)) { sr += r; } return sl + this + sr; } return this; }; // Returns a new String with the given record separator removed from the end of this (if present). String.prototype.chomp = function(str) { var n = this.length; var s = str.length; if (s == 0) { if (this.charCodeAt(n - 1) == 13 || this.charCodeAt(n - 1) == 10) { return this.substring(0, n - 1); } } else { if (this.substring(n - s, n) == str) { return this.substring(0, n - s); } } return undefined; }; // Returns a new String with the last character removed. If the string ends with \r\n, // both characters are removed. Applying chop to an empty string returns an empty string. // String.chomp is often a safer alternative, as it leaves the string unchanged if it // doesn't end in a record separator. String.prototype.chop = function() { var n = this.length; if (n 0) { if (this.charCodeAt(n - 1) == 10 this.charCodeAt(n - 2) == 13) { return this.substring(0, n - 2); } return this.chomp(this.charAt(n - 1)); } return ; }; // str parameter defines a set of characters to count. The intersection of this set // defines the characters to count in str. Any str that starts with a caret (^) is negated. // The sequence c1--c2 means all characters between c1 and c2. String.prototype.count = function(str) { var a = this.length; if (a == 0) return 0; var s = 0; var c; if (!str.length) return 0; var o = this._parseParams(str); if (o == undefined) return undefined; var p = o.compare; var except = o.except; while (--a -(-1)) { c = this.charAt(a); if ((p.indexOf(c) == -1) == except) { s++ } } return s; }; // If n is greater than the length of this, returns a new String of length n with this // left justified and r padded; otherwise, returns this. (see String.center) String.prototype.ljust = function(n, r) { var a = this.length; if (n a) { if (!r.length) r = ; var d = n - a; var sr = ; while (--d -(-1)) { sr += r; } return this + sr; } return this; }; // Returns a copy of str with all characters in the intersection of its arguments deleted. // Uses the same rules for building the set of characters as String.count String.prototype.remove = function(str) { var a = this.length; if (a == 0) return ; var s = ; var c; if (!str.length) return this; var o = this._parseParams(str); if (o == undefined) return undefined; var p = o.compare; var except = o.except; while (--a -(-1)) { c = this.charAt(a); if ((p.indexOf(c) == -1) !=
RE: [Flashcoders] String Empowerment
Right you are! Thanks! BLITZ | Steven Sacks - 310-551-0200 x209 -Original Message- From: [EMAIL PROTECTED] [mailto:flashcoders- [EMAIL PROTECTED] On Behalf Of Heerko van der Kooij Sent: Tuesday, August 01, 2006 9:12 AM To: Flashcoders mailing list Subject: Re: [Flashcoders] String Empowerment Hi Steven, Maybe i'm saying something stupid here, but would this be a prettier way to do the capitalize? String.prototype.capitalize = function() { return this.charAt(0).toUpperCase() + this.substr(1).toLowerCase(); } ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [Flashcoders] String Empowerment
Don't use the toLowerCase() Because when I write my name like: Bernard Visscher I don't want the output to be Bernard visscher with the lowercase V. That's what capitalize() does. For what you're wanting you should use toTitleCase(). ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [Flashcoders] String Empowerment
Sorry.. my mistake... -Oorspronkelijk bericht- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Steven Sacks | BLITZ Verzonden: woensdag 2 augustus 2006 0:16 Aan: Flashcoders mailing list Onderwerp: RE: [Flashcoders] String Empowerment Don't use the toLowerCase() Because when I write my name like: Bernard Visscher I don't want the output to be Bernard visscher with the lowercase V. That's what capitalize() does. For what you're wanting you should use toTitleCase(). ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [Flashcoders] String Empowerment
Maybe you can do something with this. It's an implemenataion of the Damerau-Levenshtein distance. The output is equal to the minimal number of insertions, deletions, substitutions and transpositions needed to transform one string into the other. Could be handy for a spellchecker... It's not optimized, just wrote it a few days ago for testing... private static function compare(first:String, second:String):Number { if(first == || second == ) return null; var f:Array = first.split(); var s:Array = second.split(); var dist:Array = new Array(); for(var i:Number = f.length+1 ; i--) { dist[i] = new Array(); for(var j:Number = s.length+1 ; j--) { if(i==0) dist[i][j] = j; else dist[i][j] = 0; } dist[i][0] = i; } for(var i:Number = 1 ; i = f.length ; i++) { for(var j:Number = 1 ; j = s.length ; j++) { var cost:Number = ((f[i-1]==s[j-1])?0:1); var dx:Number = dist[i-1][j]+1; var dy:Number = dist[i][j-1]+1; var dz:Number = dist[i-1][j-1] + cost; dist[i][j] = Math.min(Math.min(dx,dy),Math.min(dy,dz)); if(i 1 j 1 f[i] == s[j-1] f[i-1] == s[j]) dist[i][j] = Math.min(dist[i][j],dist[i-2][j-2]+cost); } } return dist[f.length][s.length]; } Bernard -Oorspronkelijk bericht- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Steven Sacks | BLITZ Verzonden: vrijdag 28 juli 2006 0:59 Aan: Flashcoders mailing list Onderwerp: RE: [Flashcoders] String Empowerment Pseudocode examples of usage: hello.capitalize() Hello HELLO.capitalize() Hello 123ABC.capitalize() 123abc hello.center(4) hello hello.center(20, _) ___hello hello.chomp() hello hello\n.chomp() hello hello \n there.chomp() hello \n there hello.chomp(llo) he string\r\n.chop() string string\n\r.chop() string\n string\n.chop() string string.chop() strin x.chop().chop() a = hello world a.count(lo) 5 a.count(^hello ) 3 a.count(ej-m) 4 a.count(^e-t) 3 a = hello a.ljust(4) hello a.ljust(20) hello a = hello world a.remove(l) heo word a.remove(lo ) hewrd a.remove(^aeiou) eoo a.remove(ej-m) ho word a.remove(^e-t) helloorl stressed.reverse() desserts a = hello a.rjust(4) hello a.rjust(20, -) ---hello yellow moon.squeeze() yelow mon now is the.squeeze( )now is the putters shoot balls.squeeze(m-z) puters shot balls hello world.squeeze(^ ) helo world hello.strip() hello \tgoodbye\r\n.strip() goodbye Hello.swapcase hELLO cYbEr_PuNk11.swapcase CyBeR_pUnK11 ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [SPAM] RE: [Flashcoders] String Empowerment
Just a quick note on capitalize() String.toUpperCase() and String.toLowerCase() are part of ActionScript 1 since Flash 5 Capitalize is different than toUpperCase and toLowerCase. Those convert the entire string, capitalize only uppercases the first character and then lowercases the rest of the characters. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [SPAM] RE: [Flashcoders] String Empowerment
Those convert the entire string, capitalize only uppercases the first character and then lowercases the rest of the characters. umm... Title Case as MS Word calls it... ~Arul Prasad On 7/28/06, Steven Sacks | BLITZ [EMAIL PROTECTED] wrote: Just a quick note on capitalize() String.toUpperCase() and String.toLowerCase() are part of ActionScript 1 since Flash 5 Capitalize is different than toUpperCase and toLowerCase. Those convert the entire string, capitalize only uppercases the first character and then lowercases the rest of the characters. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [SPAM] RE: [Flashcoders] String Empowerment
umm... Title Case as MS Word calls it... Title Case in Word is different than capitalize. Title Case capitalizes every word in a string. Capitalize only capitalizes the first character of the string. However, toTitleCase() would be a handy method. I'll code it. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [SPAM] RE: [Flashcoders] String Empowerment
If you REALLY wanted to be fancy, you could have it accept an array of words *not* to capitalize (unless they are the first word). For example: var lowerCaseWords:Array = [a, an, and, of, the]; trace(a tale of two cities.toTitleCase(lowerCaseWords)); trace(THE PRINCE AND THE PAUPER.toTitleCase(lowerCaseWords)); // Output: // A Tale of Two Cities // The Prince and the Pauper -- T. Michael Keesey -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Steven Sacks | BLITZ Sent: Friday, July 28, 2006 12:01 PM To: Flashcoders mailing list Subject: RE: [SPAM] RE: [Flashcoders] String Empowerment umm... Title Case as MS Word calls it... Title Case in Word is different than capitalize. Title Case capitalizes every word in a string. Capitalize only capitalizes the first character of the string. However, toTitleCase() would be a handy method. I'll code it. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [SPAM] RE: [Flashcoders] String Empowerment
Here's toTitleCase: String.prototype.toTitleCase = function() { var a = this.length; if (a == 0) return ; var c; var w; var s = ; while (--a -(-1)) { c = this.charCodeAt(a); w = this.charAt(a - 1); w = (w == || w == \n || w == \r || w == \t || a == 0); if ((c 64 c 91 !w) || (c 96 c 123 w)) c = c ^ 32; s = String.fromCharCode(c) + s; } return s; }; ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [Flashcoders] String Empowerment
Pseudocode examples of usage: hello.capitalize() Hello HELLO.capitalize() Hello 123ABC.capitalize() 123abc hello.center(4) hello hello.center(20, _) ___hello hello.chomp() hello hello\n.chomp() hello hello \n there.chomp() hello \n there hello.chomp(llo) he string\r\n.chop() string string\n\r.chop() string\n string\n.chop() string string.chop() strin x.chop().chop() a = hello world a.count(lo) 5 a.count(^hello ) 3 a.count(ej-m) 4 a.count(^e-t) 3 a = hello a.ljust(4) hello a.ljust(20) hello a = hello world a.remove(l) heo word a.remove(lo ) hewrd a.remove(^aeiou) eoo a.remove(ej-m) ho word a.remove(^e-t) helloorl stressed.reverse() desserts a = hello a.rjust(4) hello a.rjust(20, -) ---hello yellow moon.squeeze() yelow mon now is the.squeeze( )now is the putters shoot balls.squeeze(m-z) puters shot balls hello world.squeeze(^ ) helo world hello.strip() hello \tgoodbye\r\n.strip() goodbye Hello.swapcase hELLO cYbEr_PuNk11.swapcase CyBeR_pUnK11 ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [SPAM] RE: [Flashcoders] String Empowerment
Dear Steven Sacks, I greatly appreciate the efforts. Long time back we started ActionScript Standard Library project in Sourceforge (http://sourceforge.net/projects/fasl) unfortunately it was discontinued :( On those days I wrote Code.Flash MX: String.changeCase(Title || Sentence || Toggle) http://www.shockwave-india.com/blog/?archive=2002_11_01_archive.xml#83915217 Just a quick note on capitalize() String.toUpperCase() and String.toLowerCase() are part of ActionScript 1 since Flash 5 Regards, Arul Adobe Community Expert for Flash http://www.shockwave-india.com/blog __ toUpperCase (String.toUpperCase method) public toUpperCase() : String Returns a copy of the String object, with all lowercase characters converted to uppercase. The original value is unchanged. Availability: ActionScript 1.0; Flash Player 5 Returns String - A string. Example The following example creates a string with all lowercase characters and then creates a copy of that string using toUpperCase(): var lowerCase:String = lorem ipsum dolor; var upperCase:String = lowerCase.toUpperCase(); trace(lowerCase: + lowerCase); // output: lowerCase: lorem ipsum dolor trace(upperCase: + upperCase); // output: upperCase: LOREM IPSUM DOLOR An example is also found in the Strings.fla file in the ActionScript samples folder. The following list gives typical paths to this folder: a.. Windows: boot drive\Program Files\Macromedia\Flash 8\Samples and Tutorials\Samples\ActionScript b.. Macintosh: Macintosh HD/Applications/Macromedia Flash 8/Samples and Tutorials/Samples/ActionScript __ - Original Message - From: Steven Sacks | BLITZ [EMAIL PROTECTED] To: Flashcoders mailing list flashcoders@chattyfig.figleaf.com Sent: Friday, July 28, 2006 6:59 AM Subject: [SPAM] RE: [Flashcoders] String Empowerment Pseudocode examples of usage: hello.capitalize() Hello HELLO.capitalize() Hello 123ABC.capitalize() 123abc ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com