Skip to content

Instantly share code, notes, and snippets.

@ohadios
Last active October 14, 2016 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohadios/853cf63defa41fd529710b175441f982 to your computer and use it in GitHub Desktop.
Save ohadios/853cf63defa41fd529710b175441f982 to your computer and use it in GitHub Desktop.
Arguments
String myStr = stringUtil.toAlternate('THis is a test');
system.debug(myStr);
String typeName;
typeName = stringUtil.returnTypeName(date.today());
System.Debug(typeName);
typeName = stringUtil.returnTypeName('I do words');
System.Debug(typeName);
typeName = stringUtil.returnTypeName(10);
System.Debug(typeName);
typeName = stringUtil.returnTypeName(datetime.now());
System.Debug(typeName);
typeName = stringUtil.returnTypeName(10.5);
System.Debug(typeName);
public class stringUtil {
public static String toAlternate(string inStr){
String altStr='';
for (integer i = 0; i<inStr.length(); i++){
Integer curChar = inStr.charAt(i); // Let's get the current character
if(math.mod(i, 2)==0){ //If true then this is an even location
//System.Debug('Location: '+string.valueof(i)+', mod: '+string.valueof(math.mod(i, 2))+', char: '+string.valueof(curChar));
altStr+= stringUtil.convertChar(curChar, math.mod(i, 2)); // Always return lowercase
}
Else {
//System.Debug('Location: '+string.valueof(i)+', mod: '+string.valueof(math.mod(i, 2))+', char: '+string.valueof(curChar));
altStr+= stringUtil.convertChar(curChar, math.mod(i, 2)); // Always return uppercase
}
}
return(altStr);
}
public static String convertChar(Integer inChr, integer conType){
// conType = 0 : return Lowercase
// conType = 1 : return Uppercase
// conType = any other value, return unchanged
//System.debug('Inside convertChar, received inChr: '+string.valueOf(inChr)+' and conType: '+string.valueOf(conType));
List<integer> outChr = new List<Integer>();
// First let's handle conType that is different than 0 or 1, or is different than alpha char
if (conType <> 0 && conType <> 1){
//System.Debug('Provided a converstion type different than 0 or 1');
outChr.add(inChr);
}
if (65 <= inChr && inChr<=90){ // then this char is uppercase
//System.Debug('Char is uppercase');
if (conType == 1){
outChr.add(inChr);
//System.Debug('Old Char: '+string.valueOf(inChr)+', New Char: '+string.valueOf(outChr[0]));
} ELSE {
outChr.add(inChr + 32);
//System.Debug('Old Char: '+string.valueOf(inChr)+', New Char: '+string.valueOf(outChr[0]));
}
} ELSE IF (97 <= inChr && inChr <= 122){ // then this char is lowercase
//System.debug('Char is lowercase');
if (conType == 1){
outChr.add(inChr - 32);
//System.Debug('Old Char: '+string.valueOf(inChr)+', New Char: '+string.valueOf(outChr[0]));
} ELSE {
outChr.add(inChr);
//System.Debug('Old Char: '+string.valueOf(inChr)+', New Char: '+string.valueOf(outChr[0]));
}
} ELSE {outChr.add(inChr);}
//System.debug('outChr = '+string.Valueof(outChr));
return String.fromCharArray(outChr);
}
public static String returnTypeName(String myStr){
Return '"'+myStr+'" was a string';
}
public static String returnTypeName(Integer myInt){
Return '"'+String.valueof(myInt)+'" was an Integer';
}
public static String returnTypeName(id myID){
Return '"'+String.valueof(myID)+'" was an ID';
}
public static String returnTypeName(date myDT){
Return '"'+String.valueOf(myDT)+'" was a date';
}
public static String returnTypeName(datetime myDtTm){
Return '"'+String.valueOf(myDtTm)+'" was a datetime';
}
public static String returnTypeName(decimal myDec){
Return '"'+String.valueOf(myDec)+'" was a decimal';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment