Code Snippets


Str_ASC

public static float Str_ASC(
char cCharacter)
{
return (float)System.Text.ASCIIEncoding.ASCII.GetBytes(new[] { cCharacter })[0];
}

Str_ASCStr

public static float Str_ASCStr(
string sText)
{
return (float)System.Text.ASCIIEncoding.ASCII
.GetBytes(sText.ToCharArray())[0];
}

Str_ConCatDictionaryValuesLookup

public string Str_ConCatDictionaryValuesLookup(
string sStringKeysConCat,
string sElementsDelimiter,
System.Collections.Generic.Dictionary dicItems)
{
string[] arStringArray;
string sconcat = "";

try
{
if (sStringKeysConCat.IndexOf(sElementsDelimiter) > -1)
{
arStringArray = sStringKeysConCat.Split(
new string[] { sElementsDelimiter },
StringSplitOptions.None);

if (arStringArray.GetUpperBound(0) > 0)
{
for (int icount = 0; icount <= arStringArray.GetUpperBound(0); icount++)
{
sconcat += dicItems[arStringArray[icount]] + sElementsDelimiter;
}
}

return sconcat.Substring(0, sconcat.Length - 1);
}
else
{
return dicItems[sStringKeysConCat];
}
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return string.Empty;
}
}

Str_ConCatToArrayInteger

public int[] Str_ConCatToArrayInteger(
string sStringConCat,
string sItemDelimiter)
{
string[] arStringArray;
int[] arIntegerArray;

try
{
arStringArray = sStringConCat.Split(
new string[] { sItemDelimiter },
StringSplitOptions.None);

arIntegerArray = new int[arStringArray.GetUpperBound(0) + 1];

for (int icount = 0; icount <= arStringArray.GetUpperBound(0); icount++)
{
if (arStringArray[icount].Trim().Length > 0)
{
arIntegerArray[icount] = Convert.ToInt32(arStringArray[icount]);
}
}

return arIntegerArray;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return null;
}
}

Str_ConCatToArrayList

public System.Collections.ArrayList Str_ConCatToArrayList(
string sStringConCat,
string sItemDelimiter)
{
string[] arStringArray;
System.Collections.ArrayList arArrayList;

try
{
arStringArray = sStringConCat.Split(
new string[] { sItemDelimiter },
StringSplitOptions.None);

arArrayList = new System.Collections.ArrayList();

if (arStringArray.Length > 0)
{
for (int icount = 0; icount <= arStringArray.GetUpperBound(0); icount++)
{
arArrayList.Add(arStringArray[icount]);
}
}

return arArrayList;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return null;
}
}

Str_ConCatToArrayString

public Array Str_ConCatToArrayString(
string sStringConCat,
string sItemDelimiter)
{
string[] arStringArray;

try
{
arStringArray = sStringConCat.Split(
new string[] { sItemDelimiter },
StringSplitOptions.None);

return arStringArray;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return null;
}
}

Str_ConCatToComboBox

public static void Str_ConCatToComboBox(
string sText,
System.Windows.Forms.ComboBox objComboBox,
string sSeparateChar = ";",
string sRowChar = "#",
bool bSelectIfOne = true,
bool bClearList = true)
{
try
{
if (gbEND_GENERAL)
return;

int iseparatepos;
string seachone;

if (bClearList)
objComboBox.Items.Clear();

while (sText.Length > 0)
{
iseparatepos = sText.IndexOf(sSeparateChar, StringComparison.Ordinal);

if (iseparatepos > -1)
{
objComboBox.Items.Add(sText.Substring(0, iseparatepos));
sText = sText.Substring(iseparatepos + sSeparateChar.Length);
}
else
{
objComboBox.Items.Add(sText);
sText = "";
}
}

if (objComboBox.Items.Count == 1 && bSelectIfOne)
objComboBox.SelectedIndex = 0;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ToComboBox",
msCLASSNAME,
"transfer the concatenated string to the control '" +
objComboBox.ToString() + "'.",
mobjCOMException,
mobjException);
}
}
}

Str_ConCatToDictionaryValues

public Dictionary Str_ConCatToDictionaryValues(
string sStringConCat,
string sElementsDelimiter,
string sItemsDelimiter,
bool bFirstItemInBrackets = false,
bool bFirstItemAsKeySecondAsValue = false,
bool bSecondItemAsKeyFirstAsValue = false,
bool bItemsAsKeyAndValue = false)
{
Dictionary adicReturn;
string[] arStringArray;
int icount;
int ifind;
string skey = "";
string svalue = "";

try
{
arStringArray = sStringConCat.Split(
new string[] { sElementsDelimiter },
StringSplitOptions.None);

adicReturn = new Dictionary();

if (arStringArray.GetUpperBound(0) > 0)
{
for (icount = 0; icount <= arStringArray.GetUpperBound(0) - 1; icount++)
{
ifind = arStringArray[icount].IndexOf(sItemsDelimiter);

if (ifind > -1)
{
if (bFirstItemInBrackets)
{
skey = arStringArray[icount].Substring(ifind + 1);
svalue = arStringArray[icount].Substring(ifind + 1)
+ " (" + arStringArray[icount].Substring(0, ifind) + ")";
}

if (bFirstItemAsKeySecondAsValue)
{
skey = arStringArray[icount].Substring(0, ifind);
svalue = arStringArray[icount].Substring(ifind + 1);
}

if (bSecondItemAsKeyFirstAsValue)
{
skey = arStringArray[icount].Substring(ifind + 1);
svalue = arStringArray[icount].Substring(0, ifind);
}

if (bItemsAsKeyAndValue)
{
skey = arStringArray[icount];
svalue = arStringArray[icount];
}
}

adicReturn.Add(skey, svalue);
}
}

return adicReturn;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return null;
}
}

Str_ConCatToFile

public static void Str_ConCatToFile(
string sString,
string sFolderPath,
string sFileName = "",
string sExtension = "",
bool bOverWrite = true)
{
try
{
if (gbEND_GENERAL)
return;

System.IO.StreamWriter objStreamWriter;
string sfullpath;

sfullpath = sFolderPath + sFileName + sExtension;

if (clsFile.Exists(sfullpath) && bOverWrite == false)
{
// Do nothing (VB intentionally leaves this empty)
}
else
{
objStreamWriter = new System.IO.StreamWriter(sfullpath);
objStreamWriter.WriteLine(sString);
objStreamWriter.Close();
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ToFile",
msCLASSNAME,
"return the contents of this text file as a string.",
mobjCOMException,
mobjException);
}
}
}

Str_ConCatToListBox

public static void Str_ConCatToListBox(
string sText,
System.Windows.Forms.ListBox cboListBox,
string sSeparateChar = ";",
string sRowChar = "#",
bool bSelectIfOne = true,
bool bClearList = true)
{
try
{
if (gbEND_GENERAL)
return;

int iseparatepos;
string seachone;

if (bClearList)
cboListBox.Items.Clear();

while (sText.Length > 0)
{
iseparatepos = sText.IndexOf(sSeparateChar, StringComparison.Ordinal);

if (iseparatepos > -1)
{
cboListBox.Items.Add(sText.Substring(0, iseparatepos));
sText = sText.Substring(iseparatepos + sSeparateChar.Length);
}
else
{
cboListBox.Items.Add(sText);
sText = "";
}
}

if (cboListBox.Items.Count == 1 && bSelectIfOne)
cboListBox.SelectedIndex = 0;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ToListBox",
msCLASSNAME,
"transfer the concatenated string to the control '" +
cboListBox.Name + "'.",
mobjCOMException,
mobjException);
}
}
}

Str_FormatDateTime

public string Str_FormatDateTime(
string sDateString,
string sDateFormat)
{
string sreturn = string.Empty;
DateTime dtdatetime;

try
{
if (My.Settings.USER_ERROR_OCCURRED)
return string.Empty;

if (sDateString.Length == 0)
return string.Empty;

dtdatetime = String_ToDateTime(sDateString);

return dtdatetime.ToString(sDateFormat);
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return string.Empty;
}
}

Str_IsBlank

public bool Str_IsBlank(
string sText)
{
try
{
return (sText == "") || (sText == " ");
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return false;
}
}

Str_IsNumeric

public bool Str_IsNumeric(
string sText)
{
char sChar;
short Base;

try
{
foreach (char c in sText)
{
sChar = c;
Base = Convert.ToInt16(sChar);

if (!((Base <= 57) && (Base >= 48)))
{
return false;
}
}

return true;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return false;
}
}

Str_RemoveAppendedBrackets

public string Str_RemoveAppendedBrackets(
string sText,
bool bOnlyRemoveNumeric)
{
string sNewText = "";
int istart;
int ifinish;

try
{
if (sText.Length == 0)
return string.Empty;

sNewText = sText;

istart = sNewText.IndexOf("(");

if (istart > -1)
{
ifinish = sNewText.LastIndexOf(")");

if (bOnlyRemoveNumeric)
{
string inner = sNewText.Substring(istart + 1, ifinish - (istart + 1));

if (modGeneral.String_IsNumeric(inner))
{
return sNewText.Substring(0, ifinish - 1).Trim();
}
else
{
return sNewText;
}
}

return sNewText.Substring(0, ifinish - 1).Trim();
}
else
{
return sNewText;
}
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return string.Empty;
}
}

Str_RemoveLastDoubleReturn

public string Str_RemoveLastDoubleReturn(
string sText)
{
string sNewText;

try
{
if (sText.Length == 0)
return string.Empty;

sNewText = sText;

while (sNewText.Substring(sNewText.Length - 1, 1) == Convert.ToChar(13).ToString() &&
sNewText.Substring(sNewText.Length - 2, 1) == Convert.ToChar(13).ToString())
{
sNewText = sNewText.Substring(0, sNewText.Length - 1);
}

return sNewText;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return string.Empty;
}
}

Str_RemoveSmartQuotes

public string Str_RemoveSmartQuotes(
string sText)
{
try
{
sText = sText.Replace(Convert.ToChar(145), Convert.ToChar(39));
sText = sText.Replace(Convert.ToChar(146), Convert.ToChar(39));
sText = sText.Replace(Convert.ToChar(147), Convert.ToChar(34));
sText = sText.Replace(Convert.ToChar(148), Convert.ToChar(34));

return sText;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return string.Empty;
}
}

Str_ReturnNumberInBrackets

public int Str_ReturnNumberInBrackets(
string sText)
{
int inumber;
string snumber;
string sreturn;
int ipos_start;
int ipos_end;

try
{
sreturn = sText;

ipos_start = sreturn.IndexOf("(");
sreturn = sreturn.Substring(ipos_start + 1);

ipos_end = sreturn.IndexOf(")");
snumber = sreturn.Substring(0, ipos_end);

inumber = Convert.ToInt32(snumber);

return inumber;
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);
}

// VB returns default(Integer) = 0 when no explicit return is given
return 0;
}

Str_Reverse

Returns a string with all the characters reversed.
public string Str_Reverse(
string sText)
{
System.Text.StringBuilder workingValue;

try
{
if (sText.Length > 1)
{
workingValue = new System.Text.StringBuilder();

for (int iposition = sText.Length - 1; iposition >= 0; iposition--)
{
workingValue.Append(sText[iposition]);
}

return workingValue.ToString();
}
else
{
return sText;
}
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return string.Empty;
}
}

Str_ToByteArray

public byte[] Str_ToByteArray(
string sText)
{
try
{
var UniEncoding = new System.Text.UTF8Encoding(true);
byte[] BomBytes = UniEncoding.GetPreamble();
byte[] FileBytes = UniEncoding.GetBytes(sText);

var ByteList = new System.Collections.Generic.List();

ByteList.AddRange(BomBytes);
ByteList.AddRange(FileBytes);

return ByteList.ToArray();
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return null;
}
}

Str_ToDateTime

public DateTime Str_ToDateTime(
string sDateString)
{
try
{
if (My.Settings.USER_ERROR_OCCURRED)
return default(DateTime);

if (sDateString.Length == 0)
return default(DateTime);

string[] supportedFormats = new string[]
{
"yyyy-MM-dd 00:00:00.0",
"MM/dd/yyyy",
"MM/dd/yyyy hh:mm:ss",
"MM/dd/yy",
"dd/MM/yy",
"dd/MM/yyyy",
"ddMMMyyyy",
"yyyy MMM dd",
"dd-MMM-yyyy"
};

return DateTime.ParseExact(
sDateString,
supportedFormats,
System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.None);
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);
}

// VB returns Nothing ? default(DateTime) in C#
return default(DateTime);
}

Str_ToProperCase

public string Str_ToProperCase(
string sText)
{
try
{
return System.Globalization.CultureInfo.CurrentCulture
.TextInfo
.ToTitleCase(sText.ToLower());
}
catch (Exception ex)
{
modMessages.ErrorMessage(
System.Reflection.MethodBase.GetCurrentMethod(),
ex.Message);

return string.Empty;
}
}

Str_VBALeft

public static string Str_VBALeft(
string sText,
int iLength)
{
return sText.Substring(0, iLength);
}

Str_VBAMid

public static string Str_VBAMid(
string sText,
int iStart,
int iLength)
{
return sText.Substring(iStart - 1, iLength);
}

Str_VBARight

public static string Str_VBARight(
string sText,
int iLength)
{
return sText.Substring(sText.Length - iLength);
}

© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top