2013-12-19 54 views
0

我想构建一个if else语句使用char []作为特殊字符...我想让我的If语句在TextBox1包含所有字符的情况下运行。 ..如果没有,那么else语句将执行......我有运行,当我使用多个特殊字符IF语句的麻烦...任何帮助是非常赞赏...请参见下面的例子:如果else语句使用char []包含多个特殊字符C#

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

文本输入到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"); 
} 

但是...当我添加多个特殊字符(字符[]字符= { '^', '$', '=',“% ”, ';', ''};)我的代码将无法运行......

文本输入到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"); 
} 
+3

使用正则表达式的最简单方法。 – chridam

+0

您需要遍历其中一组字符,并将其标志设置为true,如果它们全都存在于其他字符集中。 – Bit

+0

@ Moo-Juice这看起来实际上是EDI/HL7 :) – cloyd800

回答

4

您可以使用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" 
} 
+0

从OP的问题来看,并不清楚,但第一个'?'和''之间有一个换行符;您需要使用'RegexOptions.Singleline'来匹配。但这正是我即将发布的内容。 – Bobson

+0

@Bobson它看起来像一个空间给我。如果您检查降价systax,它全部在一行上。 –

+0

嗯,好点。它可能取决于读卡器是否将其解释为换行符或空格,但他肯定使用了空格。 – Bobson

0

我认为你可以检查一下这种方式:

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

我不认为你可以在这里使用IndexOfAny,因为它看起来occurence任何数组字符,并且只有在NONE不存在时才返回-1。

1

string.Contains()方法返回true仅当有确切匹配。

要检查字符串对于每个字符出一组给定的至少一个出现时,你必须用一个,例如检查他们一个与String.IndexOf(...)方法。

0

这个正则表达式会匹配你的模式。

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