added count, enbase64 as well as debase64 methods

This commit is contained in:
Tobi Schäfer 2006-07-24 16:03:57 +00:00
parent 971a740a0a
commit 80b6083da7

View file

@ -8,10 +8,10 @@
* *
* Copyright 1998-2006 Helma Software. All Rights Reserved. * Copyright 1998-2006 Helma Software. All Rights Reserved.
* *
* $RCSfile: helma.String.js,v $ * $RCSfile: String.js,v $
* $Author: czv $ * $Author: czv $
* $Revision: 1.5 $ * $Revision: 1.2 $
* $Date: 2006/04/18 13:06:58 $ * $Date: 2006/04/24 07:02:17 $
*/ */
@ -340,7 +340,7 @@ String.prototype.entitize = function() {
* @return Object containing head and tail properties * @return Object containing head and tail properties
*/ */
String.prototype.embody = function(limit, clipping, delimiter) { String.prototype.embody = function(limit, clipping, delimiter) {
if (typeof(limit) == "string") if (typeof limit == "string")
limit = parseInt(limit, 10); limit = parseInt(limit, 10);
var result = {head: this, tail: ""}; var result = {head: this, tail: ""};
if (!limit || limit < 1) if (!limit || limit < 1)
@ -607,6 +607,38 @@ String.prototype.isEmail = function() {
}; };
/**
* returns the amount of occurences of one string in another
*/
String.prototype.count = function(str) {
var count = 0;
var offset = 0;
while ((offset = this.indexOf(str, offset)) > -1) {
count += 1;
offset += 1;
}
return count;
};
/**
* returns the string encoded using the base64 algorithm
*/
String.prototype.enbase64 = function() {
var bytes = new java.lang.String(this) . getBytes();
return new Packages.sun.misc.BASE64Encoder().encode(bytes);
};
/**
* returns the decoded string using the base64 algorithm
*/
String.prototype.debase64 = function() {
var bytes = new Packages.sun.misc.BASE64Decoder().decodeBuffer(this);
return String(new java.lang.String(bytes));
};
// wrapper methods for string-related // wrapper methods for string-related
// global helma functions // global helma functions