STRING: ¿cómo obtener una salida de cadena de esta cadena de entrada?

How do you get the message from the following inputs

Input is formated by field name separated by comma, followed by a colon, space and then the error message.

<FieldName1>, <FieldName2>, <FieldName3>: <ErrorMessage>"     

Input Example

"ConsumerSecret, ConsumerKey: Invalid application credentials" 
"Password: Invalid Must contain at least one alpha, one numeric, and one special character"

Método

string Message GetErrorByField (string FieldName, string InputString);

1

ErrorMessage = GetErrorByField("ConsumerSecret", "ConsumerSecret, ConsumerKey: Invalid application credentials");

ErrorMessage should now equal

"Invalid application credentials".

2

ErrorMessage = GetErrorByField("ConsumerKey", "ConsumerSecret, ConsumerKey: Invalid application credentials");

ErrorMessage should now equal

"Invalid application credentials".

3

ErrorMessage = GetErrorByField("Password", "Password: Invalid Must contain at least one alpha, one numeric, and one special character");

ErrorMessage should now equal

"Invalid Must contain at least one alpha, one numeric, and one special character".

preguntado el 08 de noviembre de 11 a las 09:11

2 Respuestas

Dividir el InputString i.e., second parameter in GetErrorByField() método por : then you will get the result by considering the splitted string with index 1

string Message = InputString.Split(':')[1].Trim();

respondido 08 nov., 11:14

beautiful that works! :D, only thing the first character contains a space. - 001

Which first character are you telling about. - Sai Kalyan Kumar Akshinthala

Entonces hazlo InputString.Split(':')[1].Trim(); to trim off whitespace from the start and end. - Jeremy McGee

Puedes simplemente usar el Split method of the string class, and get the appropriate value:

GetErrorByField(string str)
{
   var splited = str.Split(":".ToCharArray());
   if (splited != null && splited.Length == 2)
      return splited[1].TrimStart().TrimEnd();
   return string.Empty;
}

Respondido el 07 de enero de 12 a las 21:01

@JeremyMcGee when question wroted at first there wasn't such an example and just says they are comma separated (first field) and : separated (second field), by current approach I'd fixed my answer, I wrote this, because OP can obtain everything he wants from different parts (but not currently). - Saeed Amiri

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.