带有C#方法参数的字符串

时间:2016-04-04 02:32:00

标签: c# string

我的句子是:!doar 12345, rayantt

使用string.Contains(!doar),我需要获取其他值:

int mount = 12345;
string destiny = "rayannt";

1 个答案:

答案 0 :(得分:1)

input成为问题中所述的输入字符串,searchString是您要搜索的字符串; strParamintParam是两个必需的输出;现在考虑以下代码:

string input = "!doar 12345, rayantt";
string searchString = "!doar";
string strParam=string.Empty ;
int intParam=0;
if (input.Contains(searchString)) // check for the existence of the search string in given string
   {
      input = input.Replace(searchString, ""); // remove the searchstring from the input
      string[] contents = input.Split(',');
      int.TryParse(contents[0], out intParam); // collect the integer param
      strParam=contents[1]; // collect the string param
   }
// here you get 12345 in intParam and "rayantt" in strParam