I've posted a few code snippets and some people noticed that there are references to unresolved methods.I am using a few helper classes. This post describes my StringHelper class.
By the way, I recently found (in the article Concatenating Strings Efficiently), that using StringBuilder is often less efficient, than simple concatenation.
using System;
using System.IO;
using System.Reflection;
using System.Text;
public class StringHelper
{
// 'See also FxLib Author: Kamal Patel, Rick Hodder
// 'Find the first entry of sToFing and returns the string after it
// 'See also FxLib StringExtract (and StuffString)
// Methods
public StringHelper()
{
}
#region "String Functions"
public static string LeftBefore(string str, string sToFing)
{
return LeftBefore(str, sToFing, false);
}
// 'if sToFind not found, then the full string should be returned //change 6/7/2005
// 'if sToFind is empty, all string should be returned
public static string LeftBefore(string str, string sToFing,bool EmptyIfNotFound)
{
StringBuilder builder1 = new StringBuilder(str);
if (sToFing.Length > 0)
{
int num1 = str.IndexOf(sToFing);
if (num1 < 0)
{
if(EmptyIfNotFound==true) return "";
else return str;// 6/7/2005 full string should be returned
}
builder1.Remove(num1, builder1.Length - num1);
}
return builder1.ToString();
}
// 'if sToFind not found, then original string should be returned
//Search is case-sensitive
public static string LeftBeforeLast(string str, string sToFing)
{
StringBuilder builder1 = new StringBuilder(str);
if (sToFing.Length > 0)
{
int num1 = str.LastIndexOf(sToFing);
if (num1 < 0)
{
return str;
}
builder1.Remove(num1, builder1.Length - num1);
}
return builder1.ToString();
}
public static string LeftBeforeLast(string str, string sToFing, int FindNumberTimes)//6/1/2006
{
for (int i = 0; i < FindNumberTimes; i++)
{
str = LeftBeforeLast(str, sToFing);
}
return str;
}
// 'if sBefore not found, then string from the beginning should be returned
// 'if sAfter not found, then string up to the end should be returned
public static string MidBetween(string str, string sBefore, string sAfter)
{
return MidBetween(str, sBefore, sAfter, false);
}
public static string MidBetween(string str, string sBefore, string sAfter,bool EmptyIfNotFound)
{
string text2 = StringHelper.RightAfter(str, sBefore,EmptyIfNotFound);
return StringHelper.LeftBefore(text2, sAfter,EmptyIfNotFound);
}
//if sToFind not found, then original string should be returned ' 6/7/2005 change
//if sToFind is empty, all string should be returned
public static string RightAfter(string str, string sToFing)
{
return RightAfter(str, sToFing, false);
}
public static string RightAfter(string str, string sToFing, bool EmptyIfNotFound)
{
StringBuilder builder1 = new StringBuilder(str);
int num1 = str.IndexOf(sToFing);
if (num1 < 0)
{
if (EmptyIfNotFound == true) return "";
else return str;
}
builder1.Remove(0, num1 + sToFing.Length);
return builder1.ToString();
}
//'Find the last entry of sToFing and returns the string after it
//if sToFind not found, then original string should be returned ' 6/7/2005 change
//if sToFind is empty, the original string should be returned
public static string RightAfterLast(string str, string sToFing)
{
StringBuilder builder1 = new StringBuilder(str);
int num1 = str.LastIndexOf(sToFing);
if (num1 < 0)
{
return str;
}
builder1.Remove(0, num1 + sToFing.Length);
return builder1.ToString();
}
public static string RightAfterLast(string str, char chToFing, int nOccurencesNumber)//6/1/2006
{ //C++ solutions to find n'th occurence see http://www.codecomments.com/forum272/message731385.html
string[] aStr= str.Split(chToFing);
StringBuilder sb=new StringBuilder() ;
int nPosInSplit=aStr.Length-nOccurencesNumber;
for (int i = nPosInSplit; i < aStr.Length; i++)
{
sb.Append(aStr[i]);
sb.Append(chToFing);
}
sb.Remove(sb.Length - 1, 1);//1 for char
return sb.ToString();
}
public static string[] SplitWithTrim(string str, char chSeparator)//8/8/2006
{
char[] aSep = new char[] { chSeparator };
string[] aStr = str.Split(aSep, StringSplitOptions.RemoveEmptyEntries);
return StringArrayHelper.Trim(aStr);
//for (int i = 0; i < aStr.Length; i++)
//{
// aStr[i]=aStr[i].Trim();
//}
//return aStr;
}
public static string[] SplitRemoveEmptyEntries(string str, char chSeparator)//11/8/2006
{
return Split(str, chSeparator, StringSplitOptions.RemoveEmptyEntries);
}
/// <summary>
/// Extra "overload" for char instead of char array
/// </summary>
/// <param name="str"></param>
/// <param name="chSeparator"></param>
/// <param name="options"></param>
/// <returns></returns>
public static string[] Split(string str, char chSeparator, StringSplitOptions options)//8/8/2006
{
char[] aSep = new char[] { chSeparator };
string[] aStr = str.Split(aSep, options);
return aStr;
}
//'Removes the start part of the string, if it is matchs, otherwise leave string unchanged
//NOTE:case-sensitive, if want case-incensitive, change ToLower both parameters before call
public static string TrimStart(string str, string sStartValue)
{
if (str.StartsWith(sStartValue))
{
str = str.Remove(0, sStartValue.Length);
}
return str;
}
// 'Removes the end part of the string, if it is matchs, otherwise leave string unchanged
public static string TrimEnd(string str, string sEndValue)
{
if (str.EndsWith(sEndValue))
{
str = str.Remove(str.Length - sEndValue.Length, sEndValue.Length);
}
return str;
}
public static string IfNotEmptyEnsureEndsWith(string str, string sEndValue)
{
if ( DataHelper.IsNullOrEmpty(str)) return str; //21/10/2005
if (!str.EndsWith(sEndValue))
{
str = str + sEndValue;
}
return str;
}
public static string EnsureEndsWith(string str, string sEndValue)
{
return EnsureEndsWith(str, sEndValue, StringComparison.CurrentCulture);
}
public static string EnsureEndsWith(string str, string sEndValue, StringComparison comparisonType)
{
if (!str.EndsWith(sEndValue, comparisonType))
{
str = str + sEndValue;
}
return str;
}
public static string AppendWithDelimeter(string str, string sToAppend, string delimeter)
{
if ((!str.EndsWith(delimeter) & !DataHelper.IsNullOrEmpty(str)) & !DataHelper.IsNullOrEmpty(sToAppend))
{
str = str + delimeter;
}
str = str + sToAppend;
return str;
}
//from http://66.102.7.104/search?q=cache:DSw2bnf_FlMJ:blogs.msdn.com/brada/archive/2004/02/16/73535.aspx+%22EndsWith+char+%22+string+C%23&hl=en
//" internal bool EndsWith(char value) in String class. Why it would be internal? Also, there is no bool StartsWith(char value). "
public static bool EndsWith(string str,char value)
{
int num1 = str.Length;
if ((num1 != 0) && (str[num1 - 1] == value))
{
return true;
}
return false;
}
public static bool StartsWith(string str,char value)
{
if ((str.Length != 0) && (str[0] == value))
{
return true;
}
return false;
}
#endregion //"String Functions"
#region "String Array Functions"
//24/10/2006 moved to StringArrayHelper
//public static string[] ToLower(string[] sArray)
//{
// for(int i=0;i<sArray.Length;i++)
// {
// sArray[i]=sArray[i].ToLower();
// }
// return sArray;
//}
#endregion //"String Array Functions"
#region "String Brackets Functions"
// 'StripBrackets checks that starts from sStart and ends with sEnd (case sensitive).
// 'If yes, than removes sStart and sEnd.
// 'Otherwise returns full string unchanges
// 'See also MidBetween
public static string StripBrackets(string str, string sStart, string sEnd)
{
if (StringHelper.CheckBrackets(str, sStart, sEnd))
{
str = str.Substring(sStart.Length, (str.Length - sStart.Length) - sEnd.Length);
}
return str;
}
public static bool CheckBrackets(string str, string sStart, string sEnd)
{
bool flag1 = false;
if ((str!=null) && (str.StartsWith(sStart) && str.EndsWith(sEnd)))//19/5/ null handling
{
flag1 = true;
}
return flag1;
}
public static string WrapBrackets(string str, string sStartBracket, string sEndBracket)
{
StringBuilder builder1 = new StringBuilder(sStartBracket);
builder1.Append(str);
builder1.Append(sEndBracket);
return builder1.ToString();
}
// 'Concatenates a specified separator String between each element of a specified String array wrapping each element, yielding a single concatenated string
public static string JoinWrapBrackets(string[] aStr, string sDelimeter, string sStartBracket, string sEndBracket)
{
StringBuilder builder1 = new StringBuilder();
string[] textArray1 = aStr;
for (int num1 = 0; num1 < textArray1.Length; num1++)
{
string text2 = textArray1[num1];
builder1.Append(StringHelper.WrapBrackets(text2, sStartBracket, sEndBracket));
builder1.Append(sDelimeter);
}
return StringHelper.TrimEnd(builder1.ToString(), sDelimeter);
}
// ' Quote the arguments, in case they have a space in them.
public static string QuotePath(string sPath)
{
return ("\"" + sPath + "\"");
}
public static string DblQuoted(string sWord)
{
sWord = Strings.Replace(sWord, "\"", "\"\"", 1, -1, CompareMethod.Binary);
return ("\"" + sWord + "\"");
}
#endregion //"String Brackets Functions"
}