Common string manipulation functions in JavaScript

January 1, 2009 by  

In the last couple of years I have gathered a lot of common JavaScript functions for string manipulations. The functions below have surely helped me develop JavaScript in MS CRM  much faster.

//*********************************************************
gMid = function(String, Start, Length){
//gMid(String, Start, Length): Returns the substring from start to start+len
	if (String == null)
	return (false);

	if (Start > String.length)
	return "";

	if (Length == null || Length.length == 0)
	return (false);

	return String.substr((Start - 1), Length);
}
//********************************************************
gInstr = function(fullString, subString){
//gInstr(fullString, subString): Returns the first location a substring within any string.  (If the character is not found, -1 is returned.)

	var a = 0;

	if (fullString == null || subString == null)
	return (false);

	fullString = fullString.toLowerCase();
	subString = subString.toLowerCase();

	a = fullString.indexOf(subString);
	if (a == -1)
	return -1;
	else
	return a + 1;

}
//*********************************************************
gLeft = function(str, n){
	//gLeft(str, n) : Returns everything left of a position in a string or word

	if (n  String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
//*********************************************************
gRight = function(str, n){
	//gRight(str, n) : Returns everything right of a position in a string or word

	if (n  String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
//*********************************************************
gLen = function(fullString){
	//gLen(fullString) : The number of characters in the string

	if (fullString == null)
	return (false);

	return String(fullString).length;
}
//*********************************************************
gLTrim = function(str){
	/*
	This function is used to left trim a string
	Usage: function(string)
	Params: str -> String you want to trim
	Returns: string
	Author: Geron Profet (www.crmxpg.nl)
	*/
	if(str+"" == "null" || str+"" == "undefined"){str="";}
	return str.replace(/^[ ]+/, '');
}
//*********************************************************
gRTrim = function(str){
	// This function is used to right trim a string
	// Usage: function(string)
	// Params: str -> String you want to trim
	// Returns: string
	// Author: Geron Profet (www.crmxpg.nl)
	if(str+"" == "null" || str+"" == "undefined"){str="";}
	return str.replace(/[ ]+$/, '');
}

//*********************************************************
gTrim = function(str){
	/*
	This function is used to trim a string
	Usage: function(string)
	Params: str -> String you want to trim
	Returns: string
	Calls: 	gLTrim()
			gRTrim()
	Author: Geron Profet (www.crmxpg.nl)
	*/
	if(str+"" == "null" || str+"" == "undefined"){str="";}
	return str.replace(/^\s+|\s+$/g,"");
}
//*********************************************************
gLeftString = function(fullString, subString) {
	//gLeftString(fullString,subString) : Returns everything Left of a string or word, excl. the search string

	//before we begin convert params touppercase in order to compare because we want to return the original string
	var fullStringUpper = fullString.toUpperCase();
	var subStringUpper =  subString.toUpperCase();

	if (fullStringUpper.indexOf(subStringUpper) == -1) {
	  return "";
	} else {
	  return (fullString.substring(0, fullStringUpper.indexOf(subStringUpper)));
	}
}
//*********************************************************
gMiddleString = function(fullString, startString, endString) {

	//gMiddleString(fullString, startString, endString) : Returns everything between two strings / words, reading from left to right

	//before we begin convert params touppercase in order to compare because we want to return the original string
	var fullStringUpper = fullString.toUpperCase();
	var startStringUpper =  startString.toUpperCase();
	var endStringUpper =  endString.toUpperCase();

   if (fullStringUpper.indexOf(startStringUpper) == -1) {
      return "";
   } else {
      var sub = fullString.substring(fullStringUpper.indexOf(startStringUpper)+startString.length, fullString.length);
	  subUpper=sub.toUpperCase();
      if (subUpper.indexOf(endStringUpper) == -1) {
         return sub;
      } else {
         return (sub.substring(0, subUpper.indexOf(endStringUpper)));
      }
   }
}
//*********************************************************
gRightString = function(fullString, subString) {
	//gRightString(fullString,subString) : Returns everything Right of a string or word, excl. the search string

	//before we begin convert params touppercase in order to compare because we want to return the original string
	var fullStringUpper = fullString.toUpperCase();
	var subStringUpper =  subString.toUpperCase();

	if (fullStringUpper.indexOf(subStringUpper) == -1) {
	return "";
	} else {
	  return (fullString.substring(fullStringUpper.indexOf(subStringUpper)+subString.length, fullString.length));
	}
}
//*********************************************************
gRightStringIncl = function(fullString, subString) {
	//gRightStringIncl(fullString,subString) : Returns everything Right of a string or word, incl. the search string

	//before we begin convert params touppercase in order to compare because we want to return the original string
	var fullStringUpper = fullString.toUpperCase();
	var subStringUpper =  subString.toUpperCase();

	if (fullStringUpper.indexOf(subStringUpper) == -1) {
	  return "";
	} else {
	  return (subString+fullString.substring(fullStringUpper.indexOf(subStringUpper)+subString.length, fullString.length));
	}
}
//*********************************************************
gReplaceString = function(strString, strReplace, strReplaceWith){

	//gReplaceString: Function to replace any string
	// When replacing plain text use single quotes e.g. gReplaceString(strString,'test','testing')

	if (strString != "" && strReplace != "") {

		var re = new RegExp(strReplace, "gi");
		var s = strString.replace(re, strReplaceWith);
		return s;
	}
	else{
		return strString;
	}
}
//*********************************************************
gReplaceStringLoop = function(input, replace, replacewith){
	// gReplaceStringLoop: Function to replace any string
	// When replacing plain text use single quotes e.g. gReplaceStringLoop(strString,'test','testing')
	for(var i = 0 ; i < input.length ; i++){
		if(input.match(replace)){
			input = input.replace(replace, replacewith);
		}
		else{
			break;
		}

		i++;
	}
	return input;
}
//*********************************************************
gReplaceSingleQuote = function(strString) {
	//gReplaceSingleQuote : Replace single quotes with ''
    if (strString != "") {
		var s = gReplaceString(strString,"'","''");
    }
	else{
		var s  = strString
	}
   return s;
}
//*********************************************************
gReplaceSpecialChars = function(string) {

	/*
	Description:	This function is used to replace special chars
	Params: 		string	= string in which to replace special chars
	Returns: 		string
	Example :		var name = gReplaceSpecialChars(name);
	Calls:			nothing
	Author: 		Geron Profet (www.crmxpg.nl)
	Version: 		1.0
	*/

   string = string.replace(/"/g,"%22");
   string = string.replace(/#/g,"%23");
   string = string.replace(/&/g,"%26");
   string = string.replace(/'/g,"%27");
   string = string.replace("*","%2A");
   string = string.replace("+","%2B");
   string = string.replace(",","%2C");
   return string;
}
//*********************************************************
gNullToString = function(strInput){
	//gNullToString(sValue) : Function to replace Null with empty string

	if(strInput+"" == "null" || strInput+"" == "undefined"){return "";}
	return strInput;
}
//*********************************************************
gNullToZero = function(strInput) {
	//gNullToZero : Function to replace Null with 0

	if(strInput+"" == "null" || strInput+"" == "undefined"){return 0;}
	return strInput;
}

Comments

2 Comments on Common string manipulation functions in JavaScript

    Nils Stritzel on Fri, 3rd Aug 2012 13:12

    You might want to check the formatting of the source code.

    Geron Profet on Thu, 18th Oct 2012 14:25

    Hello Nils,

    Code has been updated.
    Sorry for the delay.

    Best regards.