Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 修剪字符串内容以仅获取ID值_C#_String_Replace - Fatal编程技术网

C# 修剪字符串内容以仅获取ID值

C# 修剪字符串内容以仅获取ID值,c#,string,replace,C#,String,Replace,我试图找出如何修剪字符串,以便只得到ID int值。我有一个文本框,可以接受ID值,只要它们的格式像[@ID=#]9。它还可以接受BP(代表“基准价格”)和有效的数字和数学运算符。我需要做的是获取该字符串中的数字,并测试它是否为有效ID。我创建了一个函数,该函数将删除不必要的字符,只获取ID: protected int CheckValidID(string idString) { bool IsValid = false; int ValidID = 0; Strin

我试图找出如何修剪字符串,以便只得到ID int值。我有一个文本框,可以接受ID值,只要它们的格式像[@ID=#]9。它还可以接受BP(代表“基准价格”)和有效的数字和数学运算符。我需要做的是获取该字符串中的数字,并测试它是否为有效ID。我创建了一个函数,该函数将删除不必要的字符,只获取ID:

protected int CheckValidID(string idString)
{
    bool IsValid = false;
    int ValidID = 0;
    String trimmedId = idString.Trim(new Char[] { ' ', '[', '@', 'I', 'D', '=', ']' });

    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT OptionChoiceId FROM ac_OptionChoices WHERE OptionChoiceId = @OptionChoiceID";
        cmd.Parameters.AddWithValue("@OptionChoiceID", trimmedId);
        cn.Open();

        using (IDataReader reader = cmd.ExecuteReader())
        {
            if (reader.Read())
                if (reader["OptionChoiceId"].ToString() != "")
                {
                    IsValid = true;
                    ValidID = Convert.ToInt32(reader["OptionChoiceId"]);
                }

        }
        cn.Close();
    }
    return ValidID;
}
如果传入的ID有效,那么我需要将该ID替换为一个数字(任意数字)

if(PriceFormula.Text!=“”)
{
字符串pFormula=PriceFormula.Text;
if(pFormula.Contains(“@ID”))
{
CheckValidID(pFormula);
如果(校验有效性(P公式)!=0)
pFormula=pFormula.Replace(PriceFormula.Text,“2”);
}
pFormula=pFormula.替换(“BP”,“2”);
pFormula=pFormula.ToLower();
尝试
{
双n=0;
bool priceVal=(!Double.TryParse(ItemLookUp.evalExpression(pFormula.ToString(),out n));
}
捕获(异常x)
{
isValid=false;
ErrorNote.Text+=“此新选项选项的价格公式无效。
”; } }
我遇到的问题是,如果用户键入类似[@ID=#]*9的内容,这意味着它是有效的,但对我设置的内容不起作用。此外,即使用户输入了有效的ID,它也不起作用。我如何设置,以便只检索[@ID=#]的ID部分,而不管它是单独键入文本字段还是使用数字和数学运算符键入的

最后,我需要测试用户输入的内容是否实际返回有效结果(在本例中为有效的双精度)。ID或BP文本被替换为一个数字(这里我使用2),这允许测试公式。但正是身份部分让我陷入困境

文本字段的有效条目示例:

  • [@ID=532134]
  • [@ID=828718]*2
  • [@ID=347479]*3+2
  • BP*2
  • BP+6
  • BP*5+3
  • 无效条目的示例:

  • [ID=38288]
  • @ID=383793
  • [@ID=39371]+
  • BP+
  • BP*2'
  • BP+9*
  • BC*6

  • 试试正则表达式


    如果格式是给定的,那么我看不出有什么问题:

     var startIndex= inputString.IndexOf("[@ID=");
     var endIndex = inputString.LastIndexOf("]");
    
     if (startIndex < 0 || endIndex < startIndex)
         throw new ArgumentException("Format is not valid.");
    
     startIndex += "[@ID=".Length;
     var potentialIdString = inputString.Substring(startIndex, endIndex - startIndex);
     int id;
    
     if (int.TryParse(potentialIdString, out id))
          return id;
     else
         throw new ArgumentException("ID is not valid.");
    
    var startIndex=inputString.IndexOf(“[@ID=”);
    var endIndex=inputString.LastIndexOf(“]”);
    if(起始索引<0 | |结束索引<起始索引)
    抛出新ArgumentException(“格式无效”);
    startIndex+=“[@ID=”.Length;
    var-potentialistring=inputString.Substring(startIndex,endIndex-startIndex);
    int-id;
    if(int.TryParse(潜在化字符串,out id))
    返回id;
    其他的
    抛出新ArgumentException(“ID无效”);
    
    请将勾号放回(键
    `
    ,美国键盘上1键的左边)你的输入格式是什么。很难弄清楚。现在还不清楚你想在这里做什么。你能添加一些可能输入的示例字符串,以及你实际尝试提取的部分吗。有些情况可以接受,有些情况应该失败。只让文本框接受nu不是更容易吗mbers,然后在其旁边放置
    [@ID=
    ]
    文本块?我添加了有效和无效条目的示例。@这听起来是个好主意,但允许用户输入数学运算符。此外,还可以使用[@ID=and]设置文本字段书的结尾不是要求的。听起来你应该定义一个语法,使用一个lexer/parser。第二次你说“数学运算符”可能不仅仅是用正则表达式。这似乎对我有用。谢谢!
    public int CheckValidID(string idString)
    {
    
        Regex rg = new Regex(@"\d*",RegexOptions.IgnoreCase);
    
        string val = rg.Match(idString).Value;
    
        int ValidID = Convert.ToInt32(val);
    
        return ValidID;
    
    }
    
     var startIndex= inputString.IndexOf("[@ID=");
     var endIndex = inputString.LastIndexOf("]");
    
     if (startIndex < 0 || endIndex < startIndex)
         throw new ArgumentException("Format is not valid.");
    
     startIndex += "[@ID=".Length;
     var potentialIdString = inputString.Substring(startIndex, endIndex - startIndex);
     int id;
    
     if (int.TryParse(potentialIdString, out id))
          return id;
     else
         throw new ArgumentException("ID is not valid.");