Click here to Skip to main content
15,896,207 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
Super Lloyd16-Jun-16 22:54
Super Lloyd16-Jun-16 22:54 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
den2k8816-Jun-16 23:15
professionalden2k8816-Jun-16 23:15 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
Super Lloyd16-Jun-16 23:31
Super Lloyd16-Jun-16 23:31 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
den2k8816-Jun-16 23:34
professionalden2k8816-Jun-16 23:34 
JokeRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
Super Lloyd17-Jun-16 6:39
Super Lloyd17-Jun-16 6:39 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
ZurdoDev17-Jun-16 1:18
professionalZurdoDev17-Jun-16 1:18 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
BillWoodruff16-Jun-16 14:41
professionalBillWoodruff16-Jun-16 14:41 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
Marc Clifton16-Jun-16 15:20
mvaMarc Clifton16-Jun-16 15:20 
These are the string extension methods I wrote ages ago (well, since extension methods have been around):

(Note that some of this isn't very elegant, could be LINQ'ified, could be optimized, etc.)
/// <summary>
/// Returns a new string surrounded by single quotes.
/// </summary>
public static string SingleQuote(this String src)
{
    return "'" + src + "'";
}

/// <summary>
/// Returns a new string surrounded by quotes.
/// </summary>
public static string Quote(this String src)
{
    return "\"" + src + "\"";
}

/// <summary>
/// Exchanges ' for " and " for '
/// Javascript JSON support, which must be formatted like '{"foo":"bar"}'
/// </summary>
public static string ExchangeQuoteSingleQuote(this String src)
{
    string ret = src.Replace("'", "\0xFF");
    ret = ret.Replace("\"", "'");
    ret = ret.Replace("\0xFF", "\"");

    return ret;
}

/// <summary>
/// Returns the source string surrounded by a single whitespace.
/// </summary>
public static string Spaced(this String src)
{
    return " " + src + " ";
}

/// <summary>
/// Returns a new string surrounded by brackets.
/// </summary>
public static string Parens(this String src)
{
    return "(" + src + ")";
}

/// <summary>
/// Returns a new string surrounded by brackets.
/// </summary>
public static string Brackets(this String src)
{
    return "[" + src + "]";
}

/// <summary>
/// Returns a new string surrounded by brackets.
/// </summary>
public static string CurlyBraces(this String src)
{
    return "{" + src + "}";
}

/// <summary>
/// Returns everything between the start and end chars, exclusive.
/// </summary>
/// <param name="src">The source string.</param>
/// <param name="start">The first char to find.</param>
/// <param name="end">The end char to find.</param>
/// <returns>The string between the start and stop chars, or an empty string if not found.</returns>
public static string Between(this string src, char start, char end)
{
    string ret = String.Empty;
    int idxStart = src.IndexOf(start);

    if (idxStart != -1)
    {
        ++idxStart;
        int idxEnd = src.IndexOf(end, idxStart);

        if (idxEnd != -1)
        {
            ret = src.Substring(idxStart, idxEnd - idxStart);
        }
    }

    return ret;
}

public static string Between(this string src, string start, string end)
{
    string ret = String.Empty;
    int idxStart = src.IndexOf(start);

    if (idxStart != -1)
    {
        idxStart += start.Length;
        int idxEnd = src.IndexOf(end, idxStart);

        if (idxEnd != -1)
        {
            ret = src.Substring(idxStart, idxEnd - idxStart);
        }
    }

    return ret;
}

public static string BetweenEnds(this string src, char start, char end)
{
    string ret = String.Empty;
    int idxStart = src.IndexOf(start);

    if (idxStart != -1)
    {
        ++idxStart;
        int idxEnd = src.LastIndexOf(end);

        if (idxEnd != -1)
        {
            ret = src.Substring(idxStart, idxEnd - idxStart);
        }
    }

    return ret;
}

/// <summary>
/// Returns the number of occurances of "find".
/// </summary>
/// <param name="src">The source string.</param>
/// <param name="find">The search char.</param>
/// <returns>The # of times the char occurs in the search string.</returns>
public static int Count(this string src, char find)
{
    int ret = 0;

    foreach (char s in src)
    {
        if (s == find)
        {
            ++ret;
        }
    }

    return ret;
}

/// <summary>
/// Return a new string that is "around" (left of and right of) the specified string.
/// Only the first occurance is processed.
/// </summary>
public static string Surrounding(this String src, string s)
{
    return src.LeftOf(s) + src.RightOf(s);
}

public static string RightOf(this String src, char c)
{
    string ret = String.Empty;
    int idx = src.IndexOf(c);

    if (idx != -1)
    {
        ret = src.Substring(idx + 1);
    }

    return ret;
}

public static bool BeginsWith(this String src, string s)
{
    return src.StartsWith(s);
}

public static string RightOf(this String src, string s)
{
    string ret = String.Empty;
    int idx = src.IndexOf(s);

    if (idx != -1)
    {
        ret = src.Substring(idx + s.Length);
    }

    return ret;
}

/// <summary>
/// Returns everything to the right of the rightmost char c.
/// </summary>
/// <param name="src">The source string.</param>
/// <param name="c">The seach char.</param>
/// <returns>Returns everything to the right of the rightmost search char, or an empty string.</returns>
public static string RightOfRightmostOf(this string src, char c)
{
    string ret = String.Empty;
    int idx = src.LastIndexOf(c);

    if (idx != -1)
    {
        ret = src.Substring(idx + 1);
    }

    return ret;
}

/// <summary>
/// Left of the first occurance of c
/// </summary>
/// <param name="src">The source string.</param>
/// <param name="c">Return everything to the left of this character.</param>
/// <returns>String to the left of c, or the entire string.</returns>
public static string LeftOf(this string src, char c)
{
    string ret = src;
    int idx = src.IndexOf(c);

    if (idx != -1)
    {
        ret = src.Substring(0, idx);
    }

    return ret;
}

public static string LeftOf(this String src, string s)
{
    string ret = src;
    int idx = src.IndexOf(s);

    if (idx != -1)
    {
        ret = src.Substring(0, idx);
    }

    return ret;
}

/// <summary>
/// Returns null if the string is empty.
/// </summary>
public static string NullIfEmpty(this String src)
{
    return String.IsNullOrEmpty(src) ? null : src;
}

/// <summary>
/// Safe left of n chars.  If string length is less than n, returns string.
/// </summary>
public static string LeftOf(this String src, int n)
{
    return src.Length < n ? src : src.Substring(n);
}

public static string LeftOfRightmostOf(this String src, char c)
{
    string ret = src;
    int idx = src.LastIndexOf(c);

    if (idx != -1)
    {
        ret = src.Substring(0, idx);
    }

    return ret;
}

public static string LeftOfRightmostOf(this String src, string s)
{
    string ret = src;
    int idx = src.IndexOf(s);
    int idx2 = idx;

    while (idx2 != -1)
    {
        idx2 = src.IndexOf(s, idx + s.Length);

        if (idx2 != -1)
        {
            idx = idx2;
        }
    }

    if (idx != -1)
    {
        ret = src.Substring(0, idx);
    }

    return ret;
}

public static string RightOfRightmostOf(this String src, string s)
{
    string ret = src;
    int idx = src.IndexOf(s);
    int idx2 = idx;

    while (idx2 != -1)
    {
        idx2 = src.IndexOf(s, idx + s.Length);

        if (idx2 != -1)
        {
            idx = idx2;
        }
    }

    if (idx != -1)
    {
        ret = src.Substring(idx + s.Length, src.Length - (idx + s.Length));
    }

    return ret;
}

public static char Rightmost(this String src)
{
    char c = '\0';

    if (src.Length > 0)
    {
        c = src[src.Length - 1];
    }

    return c;
}

public static string TrimLastChar(this String src)
{
    string ret = String.Empty;
    int len = src.Length;

    if (len > 0)
    {
        ret = src.Substring(0, len - 1);
    }

    return ret;
}

public static bool IsBlank(this string src)
{
    return String.IsNullOrEmpty(src) || (src.Trim() == String.Empty);
}

/// <summary>
/// Returns the first occurance of any token given the list of tokens.
/// </summary>
public static string Contains(this String src, string[] tokens)
{
    string ret = String.Empty;
    int firstIndex = 9999;

    // Find the index of the first index encountered.
    foreach (string token in tokens)
    {
        int idx = src.IndexOf(token);

        if ((idx != -1) && (idx < firstIndex))
        {
            ret = token;
            firstIndex = idx;
        }
    }

    return ret;
}

public static string SplitCamelCase(this string input)
{
    // return Regex.Replace(input, "([A-Z])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim();
    // Replaced, because the version below also handles strings like "IBMMakeStuffAndSellIt", converting it to "IBM Make Stuff And Sell It"
    // See <a href="http://stackoverflow.com/questions/5796383/insert-spaces-between-words-on-a-camel-cased-token">http://stackoverflow.com/questions/5796383/insert-spaces-between-words-on-a-camel-cased-token</a>
    return Regex.Replace(
        Regex.Replace(input, @"(\P{Ll})(\P{Ll}\p{Ll})", "$1 $2"),
            @"(\p{Ll})(\P{Ll})", "$1 $2");
}

public static string CamelCase(this string src)
{
    return src[0].ToString().ToLower() + src.Substring(1).ToLower();

}

public static string PascalCase(this string src)
{
    string ret = String.Empty;

    if (!String.IsNullOrEmpty(src))
    {
        ret = src[0].ToString().ToUpper() + src.Substring(1).ToLower();
    }

    return ret;
}

/// <summary>
/// Returns a Pascal-cased string, given a string with words separated by spaces.
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
public static string PascalCaseWords(this string src)
{
    StringBuilder sb = new StringBuilder();
    string[] s = src.Split(' ');
    string more = String.Empty;

    foreach (string s1 in s)
    {
        sb.Append(more);
        sb.Append(PascalCase(s1));
        more = " ";
    }

    return sb.ToString();
}
Imperative to Functional Programming Succinctly

Contributors Wanted for Higher Order Programming Project!

Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
Sander Rossel16-Jun-16 21:10
professionalSander Rossel16-Jun-16 21:10 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
Member 1095214417-Jun-16 2:54
Member 1095214417-Jun-16 2:54 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
ZurdoDev17-Jun-16 2:57
professionalZurdoDev17-Jun-16 2:57 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
Member 1095214417-Jun-16 3:18
Member 1095214417-Jun-16 3:18 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
ZurdoDev17-Jun-16 3:21
professionalZurdoDev17-Jun-16 3:21 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
Member 1095214417-Jun-16 3:41
Member 1095214417-Jun-16 3:41 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
ZurdoDev17-Jun-16 3:49
professionalZurdoDev17-Jun-16 3:49 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
PSU Steve17-Jun-16 3:45
professionalPSU Steve17-Jun-16 3:45 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
jtmueller17-Jun-16 4:26
jtmueller17-Jun-16 4:26 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
StatementTerminator17-Jun-16 5:58
StatementTerminator17-Jun-16 5:58 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
ZurdoDev17-Jun-16 6:19
professionalZurdoDev17-Jun-16 6:19 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
StatementTerminator17-Jun-16 6:31
StatementTerminator17-Jun-16 6:31 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
ZurdoDev17-Jun-16 7:14
professionalZurdoDev17-Jun-16 7:14 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
StatementTerminator17-Jun-16 15:32
StatementTerminator17-Jun-16 15:32 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
ZurdoDev18-Jun-16 2:38
professionalZurdoDev18-Jun-16 2:38 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
Member 772330917-Jun-16 7:27
Member 772330917-Jun-16 7:27 
GeneralRe: Fine, I'll jump on the "I hate Microsoft" bandwagon Pin
ZurdoDev17-Jun-16 7:28
professionalZurdoDev17-Jun-16 7: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.