public ToString ( bool ignorePasswordChar, bool includePrompt, bool includeLiterals, int startPosition, int length ) : string | ||
ignorePasswordChar | bool | |
includePrompt | bool | |
includeLiterals | bool | |
startPosition | int | |
length | int | |
return | string |
public string ToString(bool ignorePasswordChar, bool includePrompt, bool includeLiterals, int startPosition, int length)
{
if (length <= 0)
{
return string.Empty;
}
if (startPosition < 0)
{
startPosition = 0;
//throw new ArgumentOutOfRangeException("startPosition");
}
if (startPosition >= _testString.Length)
{
return string.Empty;
//throw new ArgumentOutOfRangeException("startPosition");
}
int maxLength = _testString.Length - startPosition;
if (length > maxLength)
{
length = maxLength;
//throw new ArgumentOutOfRangeException("length");
}
if (!IsPassword || ignorePasswordChar) // we may not need to format the text...
{
if (includePrompt && includeLiterals)
{
return _testString.ToString(startPosition, length); // testString contains just what the user is asking for.
}
}
// Build the formatted string ...
StringBuilder st = new StringBuilder();
int lastPosition = startPosition + length - 1;
if (!includePrompt)
{
// If prompt is not to be included we need to replace it with a space, but only for unassigned postions below
// the last assigned position or last literal position if including literals, whichever is higher; upper unassigned
// positions are not included in the resulting string.
int lastLiteralPos = includeLiterals ? FindNonEditPositionInRange(startPosition, lastPosition, backward) : InvalidIndex;
int lastAssignedPos = FindAssignedEditPositionInRange(lastLiteralPos == InvalidIndex ? startPosition : lastLiteralPos, lastPosition, backward);
// If lastLiteralPos is in the range and lastAssignedPos is not InvalidIndex, the lastAssignedPos is the upper limit
// we are looking for since it is searched in the range from lastLiteralPos and lastPosition. In any other case
// lastLiteral would contain the upper position we are looking for or InvalidIndex, meaning all characters in the
// range are to be ignored, in this case a null string should be returned.
lastPosition = lastAssignedPos != InvalidIndex ? lastAssignedPos : lastLiteralPos;
if (lastPosition == InvalidIndex)
{
return string.Empty;
}
}
for (int index = startPosition; index <= lastPosition; index++)
{
char ch = _testString[index];
CharDescriptor chDex = _stringDescriptor[index];
switch (chDex.CharType)
{
case CharType.EditOptional:
case CharType.EditRequired:
if (chDex.IsAssigned)
{
if (IsPassword && !ignorePasswordChar)
{
st.Append(_passwordChar); // replace edit char with pwd char.
break;
}
}
else
{
if (!includePrompt)
{
st.Append(spaceChar); // replace prompt with space.
break;
}
}
goto default;
case CharType.Separator:
case CharType.Literal:
if (!includeLiterals)
{
break; // exclude literals.
}
goto default;
default:
st.Append(ch);
break;
}
}
return st.ToString();
}
MaskedTextProvider::ToString ( ) : string | |
MaskedTextProvider::ToString ( bool ignorePasswordChar ) : string | |
MaskedTextProvider::ToString ( bool includePrompt, bool includeLiterals ) : string | |
MaskedTextProvider::ToString ( bool includePrompt, bool includeLiterals, int startPosition, int length ) : string | |
MaskedTextProvider::ToString ( bool ignorePasswordChar, int startPosition, int length ) : string | |
MaskedTextProvider::ToString ( int startPosition, int length ) : string |
public static string Formatar(string valor, string mascara) { MaskedTextProvider mtpCnpj = new MaskedTextProvider(mascara); mtpCnpj.Set(valor); var formatted = mtpCnpj.ToString(); if (formatted.IndexOf(" ") == -1) return formatted; mascara = mascara.Replace("0", "#").Replace(@"\", ""); StringBuilder dado = new StringBuilder(); foreach (char c in valor) { if (Char.IsNumber(c) || c == 'x' || c == 'X') dado.Append(c); } int indMascara = mascara.Length; int indCampo = dado.Length; for (; indCampo > 0 && indMascara > 0; ) { if (mascara[--indMascara] == '#') indCampo -= 1; } StringBuilder saida = new StringBuilder(); for (; indMascara < mascara.Length; indMascara++) { saida.Append((mascara[indMascara] == '#') ? dado[indCampo++] : mascara[indMascara]); } return saida.ToString(); }