【问题标题】:if else statement using char[] containing multiple special characters C#if else 语句使用包含多个特殊字符的 char[] C#
【发布时间】:2014-01-08 07:21:25
【问题描述】:

我正在尝试使用 char[] 为特殊字符构建 if else 语句...如果 TextBox1 包含所有字符,我希望我的 If 语句运行...如果不是,则 else 语句将执行.. .当我使用多个特殊字符时,我无法运行 IF 语句...非常感谢您的帮助...请参阅以下示例:

当使用一个特殊字符(char[] chars = {'^'};)时,下面的代码将运行...

输入到 TextBox1 的文本是: %TXSMITH^SMITH$JOHN^1411 主循环^? ;95651501425264=160919780101?%

C#

char[] chars = {'^'};
string characters = new string(chars);

if (TextBox1.Text.Contains(characters))
{
    string s = TextBox1.Text;
    int beglastname = s.IndexOf("^");
    int endlastname = s.IndexOf("$", beglastname + 1);
    string lastname = s.Substring(beglastname + 1, endlastname - beglastname - 1);

    int begfirstname = s.IndexOf("$");
    int endfirstname = s.IndexOf("^", endlastname + 1);
    string firstname = s.Substring(endlastname + 1, endfirstname - begfirstname - 1);

    int begaddress = s.IndexOf("^", endlastname + 1);
    int endaddress = s.IndexOf("^", endfirstname + 1);
    string address = s.Substring(endfirstname + 1, endaddress - begaddress - 1);

    int begdob = s.IndexOf("=", begaddress + 1);
    int enddob = s.IndexOf("%", endaddress + 1);
    string dob = s.Substring(s.IndexOf("?%", endaddress + 1) - 8, 8); // DOB field is always 8 characters, should be doing the substring backwards from the padding character 8 characters in length
    string categories = firstname + " " + lastname + " " + address + " " + dob + Environment.NewLine;
    File.AppendAllText(@"C:\temp\test\dl_test2.txt", categories);
}
else
{
    Response.Redirect("error_page_c.aspx");
}

但是...当我添加多个特殊字符 (char[] chars = { '^', '$', '=', '%', ';', '?' };) 我的代码不会运行...

输入到 TextBox1 的文本是: %TXSMITH^SMITH$JOHN^1411 主循环^? ;95651501425264=160919780101?%

C#

char[] chars = { '^', '$', '=', '%', ';', '?' };;
string characters = new string(chars);

if (TextBox1.Text.Contains(characters))
{
    string s = TextBox1.Text;
    int beglastname = s.IndexOf("^");
    int endlastname = s.IndexOf("$", beglastname + 1);
    string lastname = s.Substring(beglastname + 1, endlastname - beglastname - 1);

    int begfirstname = s.IndexOf("$");
    int endfirstname = s.IndexOf("^", endlastname + 1);
    string firstname = s.Substring(endlastname + 1, endfirstname - begfirstname - 1);

    int begaddress = s.IndexOf("^", endlastname + 1);
    int endaddress = s.IndexOf("^", endfirstname + 1);
    string address = s.Substring(endfirstname + 1, endaddress - begaddress - 1);

    int begdob = s.IndexOf("=", begaddress + 1);
    int enddob = s.IndexOf("%", endaddress + 1);
    string dob = s.Substring(s.IndexOf("?%", endaddress + 1) - 8, 8); // DOB field is always 8 characters, should be doing the substring backwards from the padding character 8 characters in length
    string categories = firstname + " " + lastname + " " + address + " " + dob + Environment.NewLine;
    File.AppendAllText(@"C:\temp\test\dl_test2.txt", categories);
}
else
{
    Response.Redirect("error_page_c.aspx");
}

【问题讨论】:

  • 使用正则表达式的最简单方法。
  • 您需要遍历其中一组字符,如果它们都存在于其他字符集中,则将标志设置为 true。
  • @Moo-Juice 这实际上看起来像 EDI/HL7 :)
  • 或者某种形式的刷卡数据。信用卡看起来非常相似。

标签: c# if-statement char


【解决方案1】:

此正则表达式将匹配您的模式。

public bool IsTheStringIWant(string s)
{
    Regex regex = new Regex("%.*\\^.*\\$.*\\^.*\\^\\?.*;.*=.*\\?%");
    return regex.IsMatch(s);
}

【讨论】:

    【解决方案2】:

    你可以用一点Linq,例如:

    if (characters.All(c => TextBox1.Text.Contains(c.ToString()))
    {
        ...
    }
    

    但最好重构代码以明智地使用Split 或正则表达式。

    下面是一个使用正则表达式的例子:

    string s = "%TXSMITH^SMITH$JOHN^1411 MAIN CIRCLE^? ;95651501425264=160919780101?%";
    string pattern = @"^%.*?\^(?<ln>.*?)\$(?<fn>.+?)\^(?<addr>.+?)\^.*=\d{4}(?<dob>\d+)\?%$";
    var match = Regex.Match(s, pattern);
    if (match.Success)
    {
        string categories = 
            String.Join(" ", 
                        match.Groups["fn"], 
                        match.Groups["ln"], 
                        match.Groups["addr"], 
                        match.Groups["dob"]) 
            + Environment.NewLine;
        // categories == "JOHN SMITH 1411 MAIN CIRCLE 19780101"
    }
    

    【讨论】:

    • 从 OP 的问题中并不清楚,但在第一个 ?; 之间有一个换行符你需要使用 RegexOptions.Singleline 来匹配它。但这正是我要发布的内容。
    • @Bobson 对我来说它看起来像是一个空间。如果你检查 markdown systax,它就在一行上。
    • 嗯,好点子。这可能取决于读卡器是否将其解释为换行符或空格,但他肯定使用了空格。
    【解决方案3】:

    我想你可以这样检查:

    bool ContainsAllChars = true;
    foreach (char NextChar in chars)
        if (TextBox1.Text.IndexOf(NextChar) == -1)
        {
            ContainsAllChars = false;
            break;
        }
    if (ContainsAllChars)
        ...
    

    我认为您不能在此处使用 IndexOfAny,因为它会查找任何数组字符的出现,并且仅当它们都不存在时才返回 -1。

    【讨论】:

      【解决方案4】:

      string.Contains() 方法仅在存在 精确 匹配时返回 true

      要检查一个字符串是否在给定集合中的每个字符至少出现一次,您必须一个一个地检查它们,例如使用String.IndexOf(...) 方法。

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-09
        • 1970-01-01
        • 2013-06-01
        • 1970-01-01
        相关资源
        最近更新 更多