我想构建一个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