Code:
String.prototype.replaceall = function(whatreplace, toreplace)
{
var result = this;
while(result.match(whatreplace))
result = result.replace(whatreplace, toreplace);
return result;
}
String.prototype.parse = function(separator)
{
var result = new Array();
var pos = 0;
var pos2 = 0;
var items = 0;
var this2 = this
this2 += separator;
for(;;)
{
pos=pos2
pos2 = this2.indexOf(separator, pos2);
if (pos2 != -1)
{
result[items] = this.substring(pos, pos2);
items++;
pos2++;
}
else
break;
}
return result;
}
String.prototype.gettoken = function(index, separator)
{
var pos = 0;
var pos2 = 0;
var items = 0;
var this2 = this
this2 += separator;
for(;;)
{
pos2=pos
pos = this2.indexOf(separator, pos);
if (pos == -1)
break;
if (items+1 == index)
return this.substring(pos2, pos);
items++;
pos++;
}
return undefined;
}
String.prototype.gettokencount = function(separator)
{
var pos = 0;
var pos2 = 0;
var items = 0;
var this2 = this
this2 += separator;
for(;;)
{
pos2=pos
pos = this2.indexOf(separator, pos);
if (pos == -1)
break;
items++;
pos++;
}
return items;
}
Does anyone know some better ways to do that?
samples :
Code:
var i,j,k;
var milibefore,miliafter,mili
var stringa = new String("Hello there, this is a simple test ...");
var date = new Date()
milibefore = date.getMilliseconds();
var stringb = stringa.replaceall(" ", "_");
miliafter = date.getMilliseconds();
mili = miliafter-milibefore;
print("Performance report, milliseconds for replaceall : " + mili + "\n");
print("Replaced \" \" by \"_\"\nBefore : \""+stringa+"\"\nAfter : \""+stringb+"\"\n");
var stringc = new String("Omg I finnaly did this function lol");
print("Parsing with parse method...\n");
var date = new Date()
milibefore = date.getMilliseconds();
arraya = stringc.parse(" ");
miliafter = date.getMilliseconds();
mili = miliafter-milibefore;
print("Performance report, milliseconds for parse : " + mili + "\n");
for(i in arraya)
print(" Arraya["+i+"] = \""+arraya[i]+"\"\n");
print("Parsing with gettoken method...\n");
var arrayb = new Array();
var date = new Date()
milibefore = date.getMilliseconds();
for(j = 0; j<stringc.gettokencount(" "); j++)
arrayb[j] = stringc.gettoken(j+1, " ");
miliafter = date.getMilliseconds();
mili = miliafter-milibefore;
print("Performance report, milliseconds for gettoken : " + mili + "\n");
for(k in arrayb)
print(" Arrayb["+k+"] = \""+arrayb[k]+"\"\n");
print("Getting token number : "+stringc.gettokencount(" ")+"\n");
print("Getting token 4 : "+stringc.gettoken(4, " ")+"\n");
Quote:
Performance report, milliseconds for replaceall : 0
Replaced " " by "_"
Before : "Hello there, this is a simple test ..."
After : "Hello_there,_this_is_a_simple_test_..."
Parsing with parse method...
Performance report, milliseconds for parse : 0
Arraya[0] = "Omg"
Arraya[1] = "I"
Arraya[2] = "finnaly"
Arraya[3] = "did"
Arraya[4] = "this"
Arraya[5] = "function"
Arraya[6] = "lol"
Parsing with gettoken method...
Performance report, milliseconds for gettoken : 0
Arrayb[0] = "Omg"
Arrayb[1] = "I"
Arrayb[2] = "finnaly"
Arrayb[3] = "did"
Arrayb[4] = "this"
Arrayb[5] = "function"
Arrayb[6] = "lol"
Getting token number : 7
Getting token 4 : did
|
Code:
function onFireGameEvent ( event ){
if (!event.eventname) return;
if (event.eventname=="player_say"){
userid=Env.Player[event.userid]
var tArgs = new String(event.text);
var tArgv = tArgs.parse(" ");
var tArgc = tArgs.gettokencount();
for ( i in tArgv )
userid.print("Parsing... index "+i+" got value \""+tArgv[i]+"\"\n");
}
}
Quote:
Cr3V3TT3 : hello guys... ready to rage ??
Parsing... index 0 got value "hello"
Parsing... index 1 got value "guys..."
Parsing... index 2 got value "ready"
Parsing... index 3 got value "to"
Parsing... index 4 got value "rage"
|