19 Temmuz 2016 Salı

WPF Kredi Kartı No Mask

using System;

namespace **.Extensions
{
    public static class MaskExtension
    {
        public static string MaskCenter(this string inputString, string maskChar, int unmaskedLengthFromBegin, int unmaskedLengthFromEnd)
        {
            try
            {
                string outputString = "";

                for (int i = 0; i < inputString.Length; i++)
                {
                    if (i >= unmaskedLengthFromBegin && i < inputString.Length - unmaskedLengthFromEnd)
                    {
                        outputString += maskChar;
                    }
                    else
                    {
                        outputString += inputString.Substring(i, 1);
                    }
                }

                return outputString;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public static string MaskEndOfEachWord(this string inputString, string maskChar, int unmaskedLengthFromBegin)
        {
            try
            {
                var outputString = string.Empty;

                var unmaskedCharCount = 0;

                for (int i = 0; i < inputString.Length; i++)
                {
                    if (inputString[i] == ' ')
                    {
                        unmaskedCharCount = 0;

                        outputString += inputString[i];
                    }
                    else
                    {
                        if (unmaskedCharCount >= unmaskedLengthFromBegin)
                        {
                            outputString += maskChar;
                        }
                        else
                        {
                            outputString += inputString[i];

                            unmaskedCharCount++;
                        }
                    }
                }

                return outputString;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }
}

Hiç yorum yok: