我想構建一個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");
}
使用正則表達式的最簡單方法。 – chridam
您需要遍歷其中一組字符,並將其標誌設置爲true,如果它們全都存在於其他字符集中。 – Bit
@ Moo-Juice這看起來實際上是EDI/HL7 :) – cloyd800