lot__no是公共字符串
Dim myDelims As String() = New String() {"<beginning of record>"}
split = temp_string.Split(myDelims, StringSplitOptions.None)
For Each s As String In split
If InStr(s, lot__no) <> 0 Then...........
所有这些代码都有效,除非由于某种原因没有在s中找到lot__no,即使在调试器中我检查过一切都应该正常工作
temp_string的样本:
<beginning of record><Patient name>Alex Gordon</Patient name><Patient ID>1239871</Patient ID><Doc name>Dr. Bortz</Doc name><Lot no>928374</Lot no><highly intolerant>Blueberry*,*Cheddar Cheese*,*Chicken*,*Chili Pepper*,*Chocolate*,*Cinnamon*,*Clam*,*</highly intolerant><moderately>Beef*,*Broccoli*,*Codfish*,*Coffee*,*Cola nut*,*</moderately><lightly>Barley, Whole Grain*,*Buckwheat*,*Butter*,*Cabbage*,*Cane sugar*,*Cantaloupe*,*Carrot*,*Cashew*,*Cauliflower*,*Celery*,*</lightly><none>Almond*,*American Cheese*,*Apple*,*Avocado*,*Banana*,*Corn*,*Cottage Chesse*,*Cow's Milk*,*Crab*,*Cucumber*,*Egg, white/yolk*,*Eggplant*,*Garlic*,*Goat's Milk*,*Grape, White/Concord*,*Grapefruit*,*Green pea*,*Green pepper*,*Halibut*,*Honey*,*Lettuce, Iceberg*,*Lemon*,*Lima bean*,*Lobster*,*Malt*,*Millet*,*Mushroom*,*Mustard Seed*,*Oat*,*Olive*,*Onion*,*Orange*,*Oyster*,*Parsley*,*Peach*,*Peanut*,*Pinto bean*,*Pineapple*,*Pork*,*Potato*,*Rice*,*Rye*,*Safflower seed*,*Salmon*,*Sardine*,*Scallop*,*Sesame*,*Shrimp*,*Sole*,*Soybean*,*Spinach*,*Squashes*,*Strawberry*,*String bean*,*Sunflower seed*,*Sweet potato*,*Swiss Cheese*,*Tea, black*,*Tobacco*,*Tomato*,*Trout*,*Tuna*,*Turkey*,*Walnut, black*,*Wheat*,*Yeast, Baker's*,*Yeast, Brewer's*,*Yogurt*,*</none><beginning of record><Patient name>Patient Name</Patient name><Patient ID>Patient ID</Patient ID><Doc name>Doctor's Name</Doc name><Lot no>Lot #</Lot no><highly intolerant>Blueberry*,*</highly intolerant><moderately>Beef*,*</moderately><lightly>Barley, Whole Grain*,*</lightly><none>Almond*,*American Cheese*,*Apple*,*Avocado*,*Banana*,*Broccoli*,*Buckwheat*,*Butter*,*Cabbage*,*Cane sugar*,*Cantaloupe*,*Carrot*,*Cashew*,*Cauliflower*,*Celery*,*Cheddar Cheese*,*Chicken*,*Chili Pepper*,*Chocolate*,*Cinnamon*,*Clam*,*Codfish*,*Coffee*,*Cola nut*,*Corn*,*Cottage Chesse*,*Cow's Milk*,*Crab*,*Cucumber*,*Egg, white/yolk*,*Eggplant*,*Garlic*,*Goat's Milk*,*Grape, White/Concord*,*Grapefruit*,*Green pea*,*Green pepper*,*Halibut*,*Honey*,*Lettuce, Iceberg*,*Lemon*,*Lima bean*,*Lobster*,*Malt*,*Millet*,*Mushroom*,*Mustard Seed*,*Oat*,*Olive*,*Onion*,*Orange*,*Oyster*,*Parsley*,*Peach*,*Peanut*,*Pinto bean*,*Pineapple*,*Pork*,*Potato*,*Rice*,*Rye*,*Safflower seed*,*Salmon*,*Sardine*,*Scallop*,*Sesame*,*Shrimp*,*Sole*,*Soybean*,*Spinach*,*Squashes*,*Strawberry*,*String bean*,*Sunflower seed*,*Sweet potato*,*Swiss Cheese*,*Tea, black*,*Tobacco*,*Tomato*,*Trout*,*Tuna*,*Turkey*,*Walnut, black*,*Wheat*,*Yeast, Baker's*,*Yeast, Brewer's*,*Yogurt*,*</none><beginning of record><Patient name>Carter Smith</Patient name><Patient ID>id123123</Patient ID><Doc name>dr katz</Doc name><Lot no>lot293847</Lot no><highly intolerant>Barley, Whole Grain*,*</highly intolerant><moderately>Banana*,*Beef*,*</moderately><lightly>Avocado*,*Blueberry*,*</lightly><none>12*,*American Cheese*,*Apple*,*Broccoli*,*Buckwheat*,*Butter*,*Cabbage*,*Cane sugar*,*Cantaloupe*,*Carrot*,*Cashew*,*Cauliflower*,*Celery*,*Cheddar Cheese*,*Chicken*,*Chili Pepper*,*Chocolate*,*Cinnamon*,*Clam*,*Codfish*,*Coffee*,*Cola nut*,*Corn*,*Cottage Chesse*,*Cow's Milk*,*Crab*,*Cucumber*,*Egg, white/yolk*,*Eggplant*,*Garlic*,*Goat's Milk*,*Grape, White/Concord*,*Grapefruit*,*Green pea*,*Green pepper*,*Halibut*,*Honey*,*Lettuce, Iceberg*,*Lemon*,*Lima bean*,*Lobster*,*Malt*,*Millet*,*Mushroom*,*Mustard Seed*,*Oat*,*Olive*,*Onion*,*Orange*,*Oyster*,*Parsley*,*Peach*,*Peanut*,*Pinto bean*,*Pineapple*,*Pork*,*Potato*,*Rice*,*Rye*,*Safflower seed*,*Salmon*,*Sardine*,*Scallop*,*Sesame*,*Shrimp*,*Sole*,*Soybean*,*Spinach*,*Squashes*,*Strawberry*,*String bean*,*Sunflower seed*,*Sweet potato*,*Swiss Cheese*,*Tea, black*,*Tobacco*,*Tomato*,*Trout*,*Tuna*,*Turkey*,*Walnut, black*,*Wheat*,*Yeast, Baker's*,*Yeast, Brewer's*,*Yogurt*,*</none><beginning of record><Patient name>Patient Name</Patient name><Patient ID>Patient ID</Patient ID><Doc name>Doctor's Name</Doc name><Lot no>Lot #</Lot no><highly intolerant>Broccoli*,*Cane sugar*,*Cantaloupe*,*Carrot*,*Cashew*,*</highly intolerant><moderately>Blueberry*,*Buckwheat*,*Cabbage*,*Cheddar Cheese*,*Chicken*,*Chili Pepper*,*Chocolate*,*Cinnamon*,*</moderately><lightly>Beef*,*Butter*,*Cola nut*,*Corn*,*Cottage Chesse*,*Cow's Milk*,*</lightly><none>1*,*2*,*3*,*Avocado*,*Banana*,*Barley, Whole Grain*,*Cauliflower*,*Celery*,*Clam*,*Codfish*,*Coffee*,*Crab*,*Cucumber*,*Egg, white/yolk*,*Eggplant*,*Garlic*,*Goat's Milk*,*Grape, White/Concord*,*Grapefruit*,*Green pea*,*Green pepper*,*Halibut*,*Honey*,*Lettuce, Iceberg*,*Lemon*,*Lima bean*,*Lobster*,*Malt*,*Millet*,*Mushroom*,*Mustard Seed*,*Oat*,*Olive*,*Onion*,*Orange*,*Oyster*,*Parsley*,*Peach*,*Peanut*,*Pinto bean*,*Pineapple*,*Pork*,*Potato*,*Rice*,*Rye*,*Safflower seed*,*Salmon*,*Sardine*,*Scallop*,*Sesame*,*Shrimp*,*Sole*,*Soybean*,*Spinach*,*Squashes*,*Strawberry*,*String bean*,*Sunflower seed*,*Sweet potato*,*Swiss Cheese*,*Tea, black*,*Tobacco*,*Tomato*,*Trout*,*Tuna*,*Turkey*,*Walnut, black*,*Wheat*,*Yeast, Baker's*,*Yeast, Brewer's*,*Yogurt*,*</none>
任何人都有任何想法为什么在另一个字符串中没有检测到一个字符串?
问题比你们想的更深刻,因为我知道代码很好
谢谢大家的意见,但遗憾的是它并没有帮助。我仍然有问题
答案 0 :(得分:3)
试试这个命令行应用。为我工作。
Module Module1
Sub Main()
Dim temp_string As String = "<beginning of record><Patient name>Alex Gordon</Patient name><Patient ID>1239871</Patient ID><Doc name>Dr. Bortz</Doc name><Lot no>928374</Lot no><highly intolerant>Blueberry*,*Cheddar Cheese*,*Chicken*,*Chili Pepper*,*Chocolate*,*Cinnamon*,*Clam*,*</highly intolerant><moderately>Beef*,*Broccoli*,*Codfish*,*Coffee*,*Cola nut*,*</moderately><lightly>Barley, Whole Grain*,*Buckwheat*,*Butter*,*Cabbage*,*Cane sugar*,*Cantaloupe*,*Carrot*,*Cashew*,*Cauliflower*,*Celery*,*</lightly><none>Almond*,*American Cheese*,*Apple*,*Avocado*,*Banana*,*Corn*,*Cottage Chesse*,*Cow's Milk*,*Crab*,*Cucumber*,*Egg, white/yolk*,*Eggplant*,*Garlic*,*Goat's Milk*,*Grape, White/Concord*,*Grapefruit*,*Green pea*,*Green pepper*,*Halibut*,*Honey*,*Lettuce, Iceberg*,*Lemon*,*Lima bean*,*Lobster*,*Malt*,*Millet*,*Mushroom*,*Mustard Seed*,*Oat*,*Olive*,*Onion*,*Orange*,*Oyster*,*Parsley*,*Peach*,*Peanut*,*Pinto bean*,*Pineapple*,*Pork*,*Potato*,*Rice*,*Rye*,*Safflower seed*,*Salmon*,*Sardine*,*Scallop*,*Sesame*,*Shrimp*,*Sole*,*Soybean*,*Spinach*,*Squashes*,*Strawberry*,*String bean*,*Sunflower seed*,*Sweet potato*,*Swiss Cheese*,*Tea, black*,*Tobacco*,*Tomato*,*Trout*,*Tuna*,*Turkey*,*Walnut, black*,*Wheat*,*Yeast, Baker's*,*Yeast, Brewer's*,*Yogurt*,*</none><beginning of record><Patient name>Patient Name</Patient name><Patient ID>Patient ID</Patient ID><Doc name>Doctor's Name</Doc name><Lot no>Lot #</Lot no><highly intolerant>Blueberry*,*</highly intolerant><moderately>Beef*,*</moderately><lightly>Barley, Whole Grain*,*</lightly><none>Almond*,*American Cheese*,*Apple*,*Avocado*,*Banana*,*Broccoli*,*Buckwheat*,*Butter*,*Cabbage*,*Cane sugar*,*Cantaloupe*,*Carrot*,*Cashew*,*Cauliflower*,*Celery*,*Cheddar Cheese*,*Chicken*,*Chili Pepper*,*Chocolate*,*Cinnamon*,*Clam*,*Codfish*,*Coffee*,*Cola nut*,*Corn*,*Cottage Chesse*,*Cow's Milk*,*Crab*,*Cucumber*,*Egg, white/yolk*,*Eggplant*,*Garlic*,*Goat's Milk*,*Grape, White/Concord*,*Grapefruit*,*Green pea*,*Green pepper*,*Halibut*,*Honey*,*Lettuce, Iceberg*,*Lemon*,*Lima bean*,*Lobster*,*Malt*,*Millet*,*Mushroom*,*Mustard Seed*,*Oat*,*Olive*,*Onion*,*Orange*,*Oyster*,*Parsley*,*Peach*,*Peanut*,*Pinto bean*,*Pineapple*,*Pork*,*Potato*,*Rice*,*Rye*,*Safflower seed*,*Salmon*,*Sardine*,*Scallop*,*Sesame*,*Shrimp*,*Sole*,*Soybean*,*Spinach*,*Squashes*,*Strawberry*,*String bean*,*Sunflower seed*,*Sweet potato*,*Swiss Cheese*,*Tea, black*,*Tobacco*,*Tomato*,*Trout*,*Tuna*,*Turkey*,*Walnut, black*,*Wheat*,*Yeast, Baker's*,*Yeast, Brewer's*,*Yogurt*,*</none><beginning of record><Patient name>Carter Smith</Patient name><Patient ID>id123123</Patient ID><Doc name>dr katz</Doc name><Lot no>lot293847</Lot no><highly intolerant>Barley, Whole Grain*,*</highly intolerant><moderately>Banana*,*Beef*,*</moderately><lightly>Avocado*,*Blueberry*,*</lightly><none>12*,*American Cheese*,*Apple*,*Broccoli*,*Buckwheat*,*Butter*,*Cabbage*,*Cane sugar*,*Cantaloupe*,*Carrot*,*Cashew*,*Cauliflower*,*Celery*,*Cheddar Cheese*,*Chicken*,*Chili Pepper*,*Chocolate*,*Cinnamon*,*Clam*,*Codfish*,*Coffee*,*Cola nut*,*Corn*,*Cottage Chesse*,*Cow's Milk*,*Crab*,*Cucumber*,*Egg, white/yolk*,*Eggplant*,*Garlic*,*Goat's Milk*,*Grape, White/Concord*,*Grapefruit*,*Green pea*,*Green pepper*,*Halibut*,*Honey*,*Lettuce, Iceberg*,*Lemon*,*Lima bean*,*Lobster*,*Malt*,*Millet*,*Mushroom*,*Mustard Seed*,*Oat*,*Olive*,*Onion*,*Orange*,*Oyster*,*Parsley*,*Peach*,*Peanut*,*Pinto bean*,*Pineapple*,*Pork*,*Potato*,*Rice*,*Rye*,*Safflower seed*,*Salmon*,*Sardine*,*Scallop*,*Sesame*,*Shrimp*,*Sole*,*Soybean*,*Spinach*,*Squashes*,*Strawberry*,*String bean*,*Sunflower seed*,*Sweet potato*,*Swiss Cheese*,*Tea, black*,*Tobacco*,*Tomato*,*Trout*,*Tuna*,*Turkey*,*Walnut, black*,*Wheat*,*Yeast, Baker's*,*Yeast, Brewer's*,*Yogurt*,*</none><beginning of record><Patient name>Patient Name</Patient name><Patient ID>Patient ID</Patient ID><Doc name>Doctor's Name</Doc name><Lot no>Lot #</Lot no><highly intolerant>Broccoli*,*Cane sugar*,*Cantaloupe*,*Carrot*,*Cashew*,*</highly intolerant><moderately>Blueberry*,*Buckwheat*,*Cabbage*,*Cheddar Cheese*,*Chicken*,*Chili Pepper*,*Chocolate*,*Cinnamon*,*</moderately><lightly>Beef*,*Butter*,*Cola nut*,*Corn*,*Cottage Chesse*,*Cow's Milk*,*</lightly><none>1*,*2*,*3*,*Avocado*,*Banana*,*Barley, Whole Grain*,*Cauliflower*,*Celery*,*Clam*,*Codfish*,*Coffee*,*Crab*,*Cucumber*,*Egg, white/yolk*,*Eggplant*,*Garlic*,*Goat's Milk*,*Grape, White/Concord*,*Grapefruit*,*Green pea*,*Green pepper*,*Halibut*,*Honey*,*Lettuce, Iceberg*,*Lemon*,*Lima bean*,*Lobster*,*Malt*,*Millet*,*Mushroom*,*Mustard Seed*,*Oat*,*Olive*,*Onion*,*Orange*,*Oyster*,*Parsley*,*Peach*,*Peanut*,*Pinto bean*,*Pineapple*,*Pork*,*Potato*,*Rice*,*Rye*,*Safflower seed*,*Salmon*,*Sardine*,*Scallop*,*Sesame*,*Shrimp*,*Sole*,*Soybean*,*Spinach*,*Squashes*,*Strawberry*,*String bean*,*Sunflower seed*,*Sweet potato*,*Swiss Cheese*,*Tea, black*,*Tobacco*,*Tomato*,*Trout*,*Tuna*,*Turkey*,*Walnut, black*,*Wheat*,*Yeast, Baker's*,*Yeast, Brewer's*,*Yogurt*,*</none>"
Dim lot__no As String = "lot293847"
Dim split As String()
Dim myDelims As String() = New String() {"<beginning of record>"}
Split = temp_string.Split(myDelims, StringSplitOptions.None)
For Each s As String In split
If s.Contains(lot__no) Then
'Either If statement seems to work as expected
'If InStr(s, lot__no) <> 0 Then
Console.WriteLine("found lot# " & lot__no & " in the following string")
Console.WriteLine(s)
Console.WriteLine()
End If
Next
Console.WriteLine()
Console.WriteLine("Press a key...")
Console.ReadKey()
End Sub
End Module
答案 1 :(得分:2)
由于您没有启用Option Explicit - 请执行此操作。你的代码在我的机器上工作,所以这是我唯一能看到的问题。
Option Explicit告诉编译器在未声明变量时抛出错误。可能是您正在使用lot__no
,而变量实际上名为lot_no
。
答案 2 :(得分:2)
这可以按预期工作,所以问题不在于您发布的代码中。
Sub Main()
Dim temp_string = "<beginning of record> blah lot123 <beginning of record>lot123"
Dim lot__no = "lot123"
Dim myDelims As String() = New String() {"<beginning of record>"}
Dim Split = temp_string.Split(myDelims, StringSplitOptions.None)
For Each s As String In Split
If InStr(s, lot__no) <> 0 Then
Console.WriteLine(String.Concat("found it in ", s))
End If
Next
End Sub
答案 3 :(得分:1)
lot__no =“Lot no&gt; 928374”
然后我会做
if s.indexof(lot__no) >= 0 then
'do stuff
end if
但是因为你似乎玩xml,你应该使用内置的.net xml解析器
不要建立自己的
答案 4 :(得分:0)
如果你使用长字符串,你应该使用正则表达式,它们更快:
If Regex.IsMatch(s, lot__no) Then ...
如果字符串很短,则s.Contains很好。