|
Case pattern
/* String.prototype.encase
A simple and quick way to ensure your strings
are being displayed with the proper capitalization,
no matters how their content is capitalized.
Concerning the pattern parameter, every alphabetical
char is mapped as "capitalization mask",
while a non-alphabetical char is mapped as
"keep the source capitalization for this char".
The last pattern char is wrapped till the end of string.
Ex: trace("hELLo WoRLd".encase("A."));
Displays: HELLo WoRLd
Ex: trace("hELLo WoRLd".encase("Aa"));
Displays: Hello world
Ex: trace("hELLo WoRLd".encase("A"));
Displays: HELLO WORLD
Have fun!
- Nosferatu -
*/
String.prototype.encase = function(pattern)
{
if (typeof pattern != "string") return this;
var a="a", _a="A", z="z", _z="Z", str="", i, j, pt, ch;
String.prototype.__c = String.prototype.toString;
for (i = 0, j = Math.min(this.length, pattern.length); i < j; i++)
{
ch = this.charAt(i);
pt = pattern.charAt(i);
String.prototype.__c = pt >= _a && pt <= _z ? String.prototype.toUpperCase : pt >= a && pt <= z ? String.prototype.toLowerCase : String.prototype.toString;
str += ch.__c();
}
str += this.substring(i).__c();
delete String.prototype.__c;
return str;
}
Posted by: Nosferatu | website http://www.betrayer.it |
//////////////////////////////////////////////////////////////////////// // Written by Daniel Simard 2006-12-13 // //////////////////////////////////////////////////////////////////////// // chemextractor will divide a molecular formula in two arrays // // one containing the atomes and the other containing the number of // // each atoms in the molecular formula // //////////////////////////////////////////////////////////////////////// // three components to be introduced in chemextractor var sFormule:String = "CH3NO20"; var atome:Array = new Array(); var number:Array = new Array(); // call chemextractor(atome, number, sFormule); function chemextractor(atome:Array, number:Array, sFormule:String):Void{ var i:Number = 0; while (i<sFormule.length){ var nexti:Number = i+1; var nextnexti:Number = i+2; var nextnextnexti:Number = i+3; //case when i+1 is inexistant if(isNaN(sFormule.charCodeAt(nexti))){ atome.push(sFormule.substr(i,1)); number.push(1); i++; //case when i+1 is a number }else if(sFormule.charCodeAt(nexti)<60){ atome.push(sFormule.substr(i,1)); //case when i+2 is a number if(sFormule.charCodeAt(nextnexti,1)<60){ number.push(sFormule.substr(nexti,2)); i+=3; //case when i+2 is a letter }else{ number.push(sFormule.substr(nexti,1)); i+=2; } //case when i+1 is a capital letter }else if(sFormule.charCodeAt(nexti)>60 and sFormule.charCodeAt(nexti)<95){ atome.push(sFormule.substr(i,1)); number.push(1); i++; //case when i+1 is a small letter }else if(sFormule.charCodeAt(nexti)>95){ atome.push(sFormule.substr(i,2)); //case if i+2 is a number if(sFormule.charCodeAt(nextnexti,1)<60){ //case if i+3 is a number (max 99...:) if(sFormule.charCodeAt(nextnextnexti,1)<60){ number.push(sFormule.substr(nextnexti,2)); i+=4; }else{ number.push(sFormule.substr(nextnexti,1)); i+=3; } //case if i+2 is a letter }else { number.push(1); i+=2; } } } } trace(sFormule); trace(atome); trace(number); // Trace output // CH3NO20 // C,H,N,O // 1,3,1,20 Posted by: Daniel Simard | website http://www.danielsimard.com |
//clean a string of any unwanted character, leading,imbedded, trailing. //simply change the charCodeAt(i) != "32" to the ascii number of the //unwanted character. This one kills all spaces. function cleanThis(dirtyString) { var dirtyString:String; var tempStr:String = ""; for (var i:Number = 0; i<dirtyString.length; i++) { if (dirtyString.charCodeAt(i) != "32") { tempStr = tempStr+dirtyString.charAt(i); } } cleanString = tempStr; return cleanString; } Posted by: Gary Lee | website http://www.windchimedesign.com |
//This is probably a little slow because it uses substr but hey, it's functional :)
// Convert a string to an array (v1)
// Jesse Stratford, www.actionscript.org
String.prototype.toArray = function () {
var inArray = new Array()
for (var c=0;c<this.length;c++){
inArray.push(this.substr( c, 1 ))
}
return inArray
}
// usage example
myString = "abcedfg";
myArray = myString.toArray();
String.prototype.toArray = function () {
return this.split( '' );
}
Posted by: Jesse Stratford | website http://www.actionscript.org |
//When creating the AMFPHP-powered page
//I got some troble, because it doesn't
//support some encodings (like cirilic in my case).
//So I decided to convert my string data to numbers
//before sending it to server, and then, when recieving data,
//to convert it back to string.
//
//This func converts string to numbers
function convOut(string) {
str = new Array();
for (m=0; m<=string.length-1; m++) {
str[m] = string.charCodeAt(m);
}
return str.join("|");
}
//This one converts numbers from convOut() func back to string
function convIn(string) {
str = new String();
num = new Array();
num = string.split("|");
for (n=0; n<=num.length-1; n++) {
str += String.fromCharCode(num[n]);
}
return str;
}
//I think there are better solutions for my problem.
//If you get one then email me
Posted by: Andy | website http://www.leftshift.ru |

