【发布时间】: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