Create a gist now

Instantly share code, notes, and snippets.

anonymous /string.js
Created Feb 15, 2015

What would you like to do?
Purebasic.String to Javascript port
/**
* @description Return the first character value of the specified string.
* @param a
* @returns {Number}
* @constructor
*/
function Asc(a) {
return a.charCodeAt(0);
}
/**
* @description Converts a quad numeric number into a string, in binary format.
* @param value
* @param type
* @returns {string}
* @constructor
*/
function Bin(value, type) {
"undefined" === typeof type && (type = 13); // quad by default
switch (type) {
case 1: // byte
case 24: // ASCII
value &= 255;
break;
case 3: // word
case 25: // Unicode
value &= 65535;
break;
case 5: // long
value &= 4294967295
}
return value.toString(2);
}
/**
* @description Returns a string created with the given character value.
* @param a
* @returns {string}
* @constructor
*/
function Chr(a) {
return String.fromCharCode(a);
}
/**
* @description Returns the string converted into lower case characters.
* @param a
* @returns {string}
* @constructor
*/
function LCase(a) {
return a.toLowerCase();
}
/**
* @description Returns the original string converted into upper case characters.
* @param a
* @returns {string}
* @constructor
*/
function UCase(a) {
return a.toUpperCase();
}
/**
* @description Returns the number of occurrences of StringToCount found in String.
* @param String
* @param StringToCount
* @returns {*}
* @constructor
*/
function CountString(String, StringToCount) {
if (0 >= StringToCount.length)
return String.length + 1;
for (var c = 0, d = 0, e = StringToCount.length; ;)
if (d = String.indexOf(StringToCount, d), 0 <= d)
c++, d += e;
else
break;
return c;
}
/**
* @description Find the 'StringToFind' within the given 'String'.
* @param String
* @param StringToFind
* @param StartPosition
* @param Mode (CaseSensitive = 0 || NoCase = 1)
* @returns {*}
* @constructor
*/
function FindString(String, StringToFind, StartPosition, Mode) {
"undefined" === typeof StartPosition && (StartPosition = 1);
"undefined" === typeof Mode && (Mode = 0);
return 0 === Mode
? String.indexOf(StringToFind, StartPosition - 1) + 1
: String.toUpperCase().indexOf(StringToFind.toUpperCase(), StartPosition - 1) + 1
}
/**
* @description Inserts 'StringToInsert' into 'String' at the specified 'Position'.
* @param String
* @param StringToInsert
* @param Position
* @returns {string}
* @constructor
*/
function InsertString(String, StringToInsert, Position) {
return String.substr(0, Position - 1) + StringToInsert + String.substr(Position - 1);
}
/**
* @description Convert a signed quad number into a string.
* @param a
* @returns {string}
* @constructor
*/
function Str(a) {
return "" + a;
}
/**
* @description Converts a float number into a string.
* @param Value
* @param NbDecimal The maximum number of decimal places for the converted number. If omitted, it will be set to 10 decimal places, with removing the trailing zeros. The number will be rounded, if 'NbDecimal' is smaller than existing decimal places of 'Value'.
* @returns {string}
* @constructor
*/
function StrF(Value, NbDecimal) {
return "undefined" === typeof NbDecimal ? Value.toString() : Value.toFixed(NbDecimal);
}
/**
* @description Converts a double number into a string.
* @param Value
* @param NbDecimal The maximum number of decimal places for the converted number. If omitted, it will be set to 10 decimal places, with removing the trailing zeros. The number will be rounded, if 'NbDecimal' is smaller than existing decimal places of 'Value'.
* @returns {string}
* @constructor
*/
function StrD(Value, NbDecimal) {
return "undefined" === typeof NbDecimal ? Value.toString() : Value.toFixed(NbDecimal);
}
/**
* @description Converts a string into a quad numeric value. The string may be an integer in decimal, hexadecimal (with '$' prefix) or binary (with '%' prefix) format. The number parsing stops at the first non numeric character.
* @param a
* @returns {Number}
* @constructor
*/
function Val(a) {
return parseInt(a, 10);
}
/**
* @description Converts a string into a float value. The string must be a float in decimal format. The number parsing stops at the first non numeric character.
* @param a
* @returns {Number}
* @constructor
*/
function ValF(a) {
return parseFloat(a, 10);
}
/**
* @description Converts a string into a double value. The string must be a float in decimal format. The number parsing stops at the first non numeric character.
* @param a
* @returns {Number}
* @constructor
*/
function ValD(a) {
return parseFloat(a, 10);
}
/**
* @description Returns the specified number of characters from the right side of the string.
* @param String
* @param Length
* @returns {string}
* @constructor
*/
function Right(String, Length) {
var c = String.length;
return String.substring(c - Length, c);
}
/**
* @description Returns the specified number of characters from the left side of the string.
* @param String
* @param Length
* @returns {string}
* @constructor
*/
function Left(String, Length) {
return String.substring(0, Length);
}
/**
* @description Extracts a string of specified length from the given string.
* @param String
* @param StartPosition
* @param Length
* @returns {string}
* @constructor
*/
function Mid(String, StartPosition, Length) {
"undefined" === typeof Length && (Length = String.length);
return String.substring(StartPosition - 1, StartPosition + Length - 1);
}
/**
* @description Removes all the specified characters located in the front of a string.
* @param String
* @param Character
* @returns {string}
* @constructor
*/
function LTrim(String, Character) {
"undefined" === typeof Character && (Character = " ");
for (var c = 0, d = String.length - 1; c <= d && String.charAt(c) == Character;)
c++;
return String.substr(c);
}
/**
* @description Removes all the specified characters located at the end of a string.
* @param String
* @param Character
* @returns {string}
* @constructor
*/
function RTrim(String, Character) {
"undefined" === typeof Character && (Character = " ");
for (var c = String.length - 1; 0 < c && String.charAt(c) == Character;)
c--;
return String.substr(0, c + 1);
}
/**
* Removes all the specified characters located at the beginning and at the end of a string.
* @param String
* @param Character
* @returns {string}
* @constructor
*/
function Trim(String, Character) {
"undefined" === typeof Character && (Character = " ");
for (var c = 0, d = String.length - 1; c <= d && String.charAt(c) == Character;)
c++;
for (; d > c && String.charAt(d) == Character;)
d--;
return String.substr(c, d - c + 1);
}
/**
* @description Returns the character length of the string.
* @param a
* @returns {*}
* @constructor
*/
function Len(a) {
return a.length;
}
/**
* @description Converts a quad numeric number into a string, in hexadecimal format.
* @param Value
* @param Type
* @returns {string}
* @constructor
*/
function Hex(Value, Type) {
"undefined" === typeof Type && (Type = 13); // quad by default
switch (Type) {
case 1: // byte
case 24: // ASCII
Value &= 255;
break;
case 3: // word
case 25: // Unicode
Value &= 65535;
break;
case 5: // long
Value &= 4294967295
}
return Value.toString(16).toUpperCase();
}
/**
* @description Try to find any occurrences of 'StringToFind' in the given 'String' and replace them with 'ReplacementString'.
* @param String
* @param StringToFind
* @param ReplacementString
* @param Mode (CaseSensitive = 0 || NoCase = 1)
* @param StartPosition
* @param NbOccurrences
* @returns {*}
* @constructor
*/
function ReplaceString(String, StringToFind, ReplacementString, Mode, StartPosition, NbOccurrences) {
"undefined" === typeof Mode && (Mode = 0);
"undefined" === typeof StartPosition && (StartPosition = 1);
"undefined" === typeof NbOccurrences && (NbOccurrences = -1);
StartPosition -= 1;
for (1 == Mode && (StringToFind = StringToFind.toUpperCase()); NbOccurrences;) {
StartPosition = 1 == Mode
? String.toUpperCase().indexOf(StringToFind, StartPosition)
: String.indexOf(StringToFind, StartPosition);
if (-1 == StartPosition)
break;
String = String.substring(0, StartPosition) + ReplacementString + String.substring(StartPosition + StringToFind.length);
StartPosition += ReplacementString.length;
NbOccurrences--;
}
return String
}
/**
* @description Finds all occurrences of 'StringToRemove' within the specified 'String' and removes them.
* @param String
* @param StringToRemove
* @param Mode (CaseSensitive = 0 || NoCase = 1)
* @param StartPosition Specifies the character position to start the removing. The first character position is 1. If omitted the whole string is used.
* @param NbOccurrences Specifies how many strings should be removed before stopping the operation. If omitted, all strings are removed.
* @returns {*}
* @constructor
*/
function RemoveString(String, StringToRemove, Mode, StartPosition, NbOccurrences) {
return ReplaceString(String, StringToRemove, "", Mode, StartPosition, NbOccurrences);
}
/**
* @description Reverses all the characters in the 'String'. The last characters becomes the first characters, and vice-versa.
* @param a
* @returns {string}
* @constructor
*/
function ReverseString(a) {
var b = "", c;
for (c = a.length - 1; 0 <= c; c--)
b += a.charAt(c);
return b;
}
/**
* @description Pads a string to the right by adding extra characters to fit the specified length.
* @param String
* @param Length
* @param Character
* @returns {*}
* @constructor
*/
function RSet(String, Length, Character) {
"undefined" === typeof Character && (Character = " ");
var d = Length - String.length;
if (String.length > Length)
return String.substring(0, Length);
if (0 < d) {
Length = Character;
for (d--; d;)
d--, Length += Character;
return Length + String
}
return String;
}
/**
* @description Pads a string to the left by adding extra characters to fit the specified length.
* @param String
* @param Length
* @param Character
* @returns {*}
* @constructor
*/
function LSet(String, Length, Character) {
"undefined" === typeof Character && (Character = " ");
var d = Length - String.length;
if (String.length > Length)
return String.substring(0, Length);
if (0 < d) {
Length = Character;
for (d--; d;)
d--, Length += Character;
return String + Length;
}
return String;
}
/**
* @description Returns the string field at the specified index.
* @param String
* @param Index
* @param Delimiter
* @returns {*}
* @constructor
*/
function StringField(String, Index, Delimiter) {
String = String.split(Delimiter);
return String.length >= Index ? String[Index - 1] : "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment