Advance String Extensions In C#

Hi, have a good day this class will help you to Improve string extensions in C#,
It will be update frequently by many C# programmers.

It will ease the using of strings in C# project while coding.
For Example :
This code will allow you to get the count of capital letters, or Reverse your string.

string SomeStrings = "Some things";
MessageBox.Show(SomeStrings.CapitalLettersCount().ToString());
MessageBox.Show(SomeStrings.Reverse());

How to use:
Simply add this class to your project, and start coding :)

using System;
using System.Text.RegularExpressions;

public static class StringExtensions
{
    // Revers string
    public static string Reverse(this string inputstring)
    {
        if (string.IsNullOrWhiteSpace(inputstring))
            return string.Empty;
        char[] chars = inputstring.ToCharArray();
        Array.Reverse(chars);
        return new string(chars);
    }

    // Get small letters count
    public static int SmallLettersCount(this string inputstring)
    {
        int res = 0;
        if (string.IsNullOrWhiteSpace(inputstring))
            return res;
        foreach (char c in inputstring.ToCharArray())
        {
            if (Char.IsLower(c))
                res++;
        }
        return res;
    }

    // Get capital letters count
    public static int CapitalLettersCount(this string inputstring)
    {
        int res = 0;
        if (string.IsNullOrWhiteSpace(inputstring))
            return res;
        foreach (char c in inputstring.ToCharArray())
        {
            if (Char.IsUpper(c))
                res++;
        }
        return res;
    }

    // Detect if string contains non English charatcers 
    public static bool IsEnglishLettersAndNumbers(this string inputstring)
    {
        if (string.IsNullOrWhiteSpace(inputstring))
            return false;

        Regex regex = new Regex(@"[A-Za-z0-9 .,-=+(){}\[\]\\]");
        MatchCollection matches = regex.Matches(inputstring);

        if (matches.Count.Equals(inputstring.Length))
            return true;
        else
            return false;
    }

    /// 
    /// By Soubhi M. Hadri 
    /// This function returns array with 3 elements that represents the counts of (0=>Number , 1=>Alphabet , 2=>other Character) for the input string.
    /// may be there are many other way to write it more simple to use
    /// 
    /// 
    /// 
    public static int[] AlphaNumcount(this string inputstring)
    {
        int[] count = new int[3];
        int numberCount = 0;
        int otherCount = 0;
        int alphaCount = 0;

        char[] tmp = inputstring.ToCharArray();
        foreach (char character in tmp)
        {
            if (Char.IsNumber(character))
                numberCount++;
            else
                if (char.IsLetter(character))
                    alphaCount++;
                else
                    otherCount++;

        }
        count[0] = numberCount;
        count[1] = alphaCount;
        count[2] = otherCount;

        return count;
    }
}

Last update 30/9/2014

Leave a Reply

Your email address will not be published.