简体   繁体   English

STRING:如何从此输入字符串获取字符串输出?

[英]STRING: how to get string output from this input string?

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"

Method 方法

string Message GetErrorByField (string FieldName, string InputString);

1 1

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

ErrorMessage should now equal 现在,ErrorMessage应该等于

"Invalid application credentials".

2 2

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

ErrorMessage should now equal 现在,ErrorMessage应该等于

"Invalid application credentials".

3 3

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

ErrorMessage should now equal 现在,ErrorMessage应该等于

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

通过以下方法拆分InputStringGetErrorByField()方法中的第二个参数:然后,您将通过考虑索引为1的拆分字符串来获得结果

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

You can simply use the Split method of the string class, and get the appropriate value: 您可以简单地使用字符串类的Split方法,并获取适当的值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM