在文本框中输入的值内使用 LIKE 进行搜索

Searching with LIKE within the value entered in the textbox

该方法抛出错误,因为输入的值太长。值如下; 0121247854789652211564864456464132564641021564646543531910192039301940215646541。我需要在这样的值中获取 91-92-93 之间的值。

解决此问题的最佳方法是使用正则表达式:

string firstValue = System.Text.RegularExpressions.Regex.Match(textBox.Text, @"(?<=(91))\d{2}(?=(92))").Value;
string secondValue = System.Text.RegularExpressions.Regex.Match(textBox.Text, @"(?<=(92))\d{2}(?=(93))").Value;

其中 textBox 是条形码文本框的名称。

正则表达式方法肯定是可行的解决方案,但我认为这样的方法更适合此目的:

private static string ExtractText(string text, string firstIndicator, string secondIndicator)
{
    var firstIndicatorIndex = text.IndexOf(firstIndicator);

    var secondIndicatorIndex = text.IndexOf(secondIndicator, , firstIndicatorIndex);

    string subtext = null;

    if (firstIndicatorIndex > -1 && secondIndicatorIndex > -1 && secondIndicatorIndex > firstIndicatorIndex)
    {
        subtext = text.Substring(firstIndicatorIndex + firstIndicator.Length, secondIndicatorIndex - firstIndicator.Length - firstIndicatorIndex);
    }

    return subtext;
}

现在,通过调用此方法,您可以提取两个指标之间的文本。

编辑:长度计算不正确。