Tengo estas dos alternativas de funciones para convertir a ProperCase(). Es
decir tomar un string y llevarlo a formato de nombre propio:
ej. ProperCase("antonio RUIZ") -> "Antonio Ruiz"
Cual de estas dos funciones debe ser mas rapida:?
1)
public static string ProperCase(string stringInput)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
bool fEmptyBefore = true;
foreach (char ch in stringInput)
{
char chThis = ch;
if (Char.IsWhiteSpace(chThis))
fEmptyBefore = true;
else
{
if (Char.IsLetter(chThis) && fEmptyBefore)
chThis = Char.ToUpper(chThis);
else
chThis = Char.ToLower(chThis);
fEmptyBefore = false;
}
sb.Append(chThis);
}
return sb.ToString();
}
2)
public static string ProperCase(string cString)
{
//Create the StringBuilder
StringBuilder sb = new StringBuilder(cString);
int i,j = 0 ;
int nLength = cString.Length ;
for ( i = 0 ; i < nLength; i++)
{
//look for a blank space and once found make the next character to
uppercase
if ((i== 0) || (char.IsWhiteSpace(cString[i])))
{
//Handle the first character differently
if( i==0 ) {j=i;}
else {j=i+1;}
//Make the next character uppercase and update the stringBuilder
sb.Remove(j, 1);
sb.Insert(j, Char.ToUpper(cString[j]));
}
}
return sb.ToString();
}
Leer las respuestas