Click here to Skip to main content
15,889,838 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Handy Extension Methods

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Jun 2011CPOL 7.5K   2
public static bool IsInString(this char c, string range){ return range.Contains(c.ToString());}public static bool IsInteger(this string text){ int num; return int.TryParse(text, out num);}public static bool IsNullOrEmpty(this string str){ return...
C#
public static bool IsInString(this char c, string range)
{
    return range.Contains(c.ToString());
}
public static bool IsInteger(this string text)
{
    int num;
    return int.TryParse(text, out num);
}
public static bool IsNullOrEmpty(this string str)
{
    return string.IsNullOrEmpty(str);
}
public static string IsNull(this string str, string defaultValue)
{
    return str.IsNullOrEmpty() ? defaultValue : str;
}
public static bool IsNumber(this string text)
{
    float num;
    return float.TryParse(text, out num);
}
public static bool IsUnicode(this string str)
{
    byte[] unicodeBytes = Encoding.Unicode.GetBytes(str);
    for (int i = 1; i < unicodeBytes.Length; i += 2)
        if (unicodeBytes[i] != 0)
            return true;
    return false;
}
public static string SqlEncodingToUtf(this string obj)
{
    return Encoding.UTF8.GetString(Encoding.GetEncoding(1256).GetBytes(obj));
}
public static string ToHex(string str)
{
    var sb = new StringBuilder();
    byte[] bytes = IsUnicode(str) ? Encoding.Unicode.GetBytes(str) : 
                   Encoding.ASCII.GetBytes(str);
    for (int counter = 0; counter < bytes.Length; counter++)
        sb.Append(bytes[counter].ToString("X"));
    return sb.ToString();
}
public static IEnumerable<int> ToInt(IEnumerable<string> array)
{
    return array.Where(str => str.IsNumber()).Select(str => str.ToInt());
}
public static string ToUnicode(this string str)
{
    return Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(str));
}
public static bool EndsWithAny(this string str, IEnumerable<string> values)
{
    return values.Any(str.EndsWith);
}
public static bool StartsWithAny(this string str, IEnumerable<string> values)
{
    return values.Any(str.StartsWith);
}
public static IEnumerable<int> AllIndexesOf(this string str, 
              string value, bool ignoreCase = false)
{
    string buffer = ignoreCase ? str.ToLower() : str;
    string stat = ignoreCase ? value.ToLower() : value;
    int statIndex = 0;
    int currIndex;
    while ((currIndex = buffer.IndexOf(stat)) != -1)
    {
        statIndex += currIndex;
        yield return statIndex;
        statIndex += stat.Length;
        buffer = buffer.Substring(currIndex + stat.Length);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Iran
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWhy not just use ToString("X") instead of creating all that ... Pin
Chris Steenkamp14-Jun-11 6:30
Chris Steenkamp14-Jun-11 6:30 
GeneralYou can avoid the "c.ToString()" in your IsInString method: ... Pin
Richard Deeming8-Jun-11 6:28
mveRichard Deeming8-Jun-11 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.