2013-12-19 58 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); 
}