Tag Archive for String

AS3 Load External Text File

package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class Main extends MovieClip
	{
		private var _txtFileToLoad:String = "myTextFile.txt";
		private var _txtLoader:URLLoader;
		private var _loadedTxt:String;
		

		public function Main():void
		{
			loadExternalTxtFile();
		}
		
		private function loadExternalTxtFile():void
		{
			_txtLoader = new URLLoader();
			_txtLoader.addEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.addEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_txtLoader.load(new URLRequest(_txtFileToLoad));
		}
		
		private function onTxtLoader_COMPLETE(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_loadedTxt = String(event.target.data);
			trace(_loadedTxt);
		}
		
		private function onTxtLoader_IO_ERROR(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			trace(event.target.text);
		}

	}
}

source

substr.c

// Extract a substring from an existing string, return the length of the substring
// dst: receive the substring
// src: the source string to extract from
// start: extract start with the 'src' + 'start' char, zero based; no bound check
// len: length of the substring; may be shorter than expected.
// Ansi string only!

int substr(char* dst, char* src, int start, int len)
{
	char* d = dst;
	char* s = src + start;
	while (len-- > 0)
	{
		if (*s == ' ')
		{
			*d = '';
			return d - dst; 
		}
		*d++ = *s++;
	}
	*d = ' ';
	return d - dst;
}

int main()
{
	char s[1024] = { ' ' };
	printf("%dt%sn", substr(s, "0123456789", 3, 10), s);
}

source

Hex encode & decode String prototype

String.prototype.hexDecode = function(){var r='';for(var i=0;i<this.length;i+=2){r+=unescape('%'+this.substr(i,2));}return r;}
String.prototype.hexEncode = function(){var r='';var i=0;var h;while(i<this.length){h=this.charCodeAt(i++).toString(16);while(h.length<2){h=h;}r+=h;}return r;}

source

isLetter(String)

Public Function isLetter(str As String)
  isLetter = (InStr(1, "abcdefghijklmnopqrstuvwxyz", LCase(str)) > 0)
End Function

source

Retrieve Query String Variables Javascript

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[[]/,"[").replace(/[]]/,"]");
  var regex = new RegExp("[?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

source

Deleted Chars

//"abc" "bc" -> [ 0 ]
//"abc" "c" -> [ 0, 1 ]
//"abac" -> "aac" -> [ 1 ]
//"aabc" -> "abc" -> [ 0 ] *or* [ 1 ]

public static List getDeletedChar(String a, String b) {
        List results = new ArrayList();
        int charIndex;
        int cursorPos = 0;
        for (int i = 0; i < b.length(); i++) {
            charIndex = a.indexOf(b.charAt(i), cursorPos);
            for (int j = cursorPos; j < charIndex; j++) {
                results.add(j);
            }
            cursorPos = charIndex + 1;
        }
        while(cursorPos < a.length()){
            results.add(cursorPos++);
        }

        return results;
    }

source

Fetch $_GET array as a string representation

<?php
/**
 * Fetches back a string representation of the current $_GET array.
 *
 * @version 1.0
 * @author Andrew Hart
 *
 * @param array $exclusions[optional] Which items of the $_GET array to skip.
 * @return string The $_GET array in string format, eg: "action=add&value=test&this=true"
 */
function fetch_query_string($exclusions = array())
{
	$str = "";
	
	foreach ($_GET as $key => $val)
	{
		if (!in_array($key, $exclusions))
		{
			$str .= $key . "=" . $val . "&";
		}
	}
	
	$str = substr($str, 0, strlen($str) - 1);
	
	return $str;
}
?>

source

Javascript – Reverse a String

var aString = "Hotel";
var rString = "";
for(var i = aString.length; i>=0; i--){
        rString += aString.substr(i,1);
}

source

SHORTEN STRING AFTER XX LETTERS AND ADD A DELIMITER (…)

function shortenString(string, maxlen, del) {  
	string = string.toString();
	if (!del) { del='...' }
	if (string.length > maxlen) {
		string = string.substr(0, maxlen) + del;     
	}  
	return string; 
}  
 
shortenString('some functions are just useful', 10)

source

replace a string

(define string-replace 
   (lambda (s match replacement)
      (let ((ll (string->list s))
            (match1 (char match))
            (replacement1 (char replacement)))
         (if (= (string-length match) 1)
             (let ((z (map (lambda (x)
                              (if (equal? x match1)
                                  replacement1
                                  x))
                           ll)))
                (list->string (list-flatten z)))
                ;z)
             (print "i can match only single characters for now")))))

source