Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

shiftChars.as

Name: shiftChars() shifts letters a-z by number(s) ie. a + 1 = b etc. add or subtract
Author: senocular: www.senocular.com
Date: 1899-12-31T00:05:10.500
Documentation:
String SHIFTCHARS: takes characters in a string (a-z and/or A-Z)
and changes their values based on a number or array of numbers as
an additive process. "a" shifted by 3 is "d"; "a" + 3 = "d"...
After z, it loops back around to a and vise versa for negative
shifting. Uppercase characters stay uppercase, lowercase stay
lowercase, non a-z, A-Z charaters are ignored.
 
Arguments:
- shiftBy: a number or array from which the characters in the
string are shifted. If a single number is passed, all a-z,A-Z
characters are shifted by that amount. If an array is passed, then the arrays
values are added to the cooresponding letters based on their positions in the
string/array. ie myArray[3] gets added to myString.charAt(3). If an array is
passed which is shorter than the string, shiftchars will only go through the 
length of that array and then quit, ignoring the rest of the string.  If the array
is greater in length, shiftchars will quit after reaching the end of the string
ignoring the remaining array values.
- startPos: (optional) starting position in the string from which the
shifting will start. Characters before this position in the string are
ignored while the startPos position in the string cooresponds to (if
array myArray is passed) myArray[0]. Defualt is 0.
  
Hints:
- if you only want to change 1 character in a string by a specific value, pass in
an array of 2 numbers (with startpos of desired char), with the first number of the
array being the shift, and the second 0. Passing 1 number will shift
all characters at and following the startPos while doing the array will shift the
desired number accordingly and the next by 0 and thats all.(see below)
 
Warnings:
- this is a per character operation.  May be slow for long strings.

Example:
letter = "a";
trace(letter.shiftChars(1)); // b
trace(letter.shiftChars(5)); // f
 
phrase = "ABC,xyz";
trace(phrase.shiftChars(1)); // BCD,yza
trace(phrase.shiftChars(1,4)); // ABC,yza
trace(phrase.shiftChars([-1,-2,-3])); // ZZZ,xyz
trace(phrase.shiftChars([1,2,3],4)); // ABC,yac
 
secretMsg = "This is a secret, OK?";
secretCode = 10;
secret = secretMsg.shiftChars(secretCode);
trace(secret); // Drsc sc k combod, YU?
msg = secret.shiftChars(-secretCode);
trace(msg); // This is a secret, OK?
 
allAs = "aaazaaa";
trace(allAs.shiftChars([1,0],3)); // aaaaaaa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
String.prototype.shiftChars = function(shiftBy, startPos){
	if (!arguments.length) return trace("Error: shiftChars - requires at least 1 parameter.");
	else if (arguments.length == 1) var startPos = 0;
	if (typeof(shiftBy) == "number") shiftBy = [shiftBy];
	var  l = this.length;
	if (startPos >= l) return this;
	var i, cas, cod, sl = shiftBy.length, str = "" + this.substr(0,startPos);
	for (i=startPos; i<l; i++){
		cod = this.charCodeAt(i);
		if (cod > 96 && cod < 123) cod -= cas = 96;
		else if (cod > 64 && cod < 91) cod -= cas = 64;
		else cas = 0;
		if (cas){
			if (sl == 1) cod = (cod + shiftBy[0] - 1) % 26;
			else if (sl >= i-startPos) cod = (cod + shiftBy[i-startPos] - 1) % 26;
			else break;
			if (cod < 0) cod += 26;
			str += String.fromCharCode(cod+cas+1);
		}else str += this.charAt(i);
	}
	if (i < l) str += this.substr(i);
	return str;
}