vb.net: weird behavior with getting position of string
lot__no is a public 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 InStr(s, lot__no) <> 0 Then...........
all of this code works, except it is not finding lot__no in s for some reason, even though in the debugger i checked everything should be working
a sample of 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*,*Buck开发者_Python百科wheat*,*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>
anyone have any ideas why one string is not detected in the other?
the problem is a deeper one than you guys think because i do know that the code is fine
thank you everyone for your input but unfortunately it has not been helpful. i am still having the problem
Try this command line app. Worked for me.
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
Since you don't have Option Explicit turned on - do it. Your code works on my machine, so this is the only thing I can see as being a problem.
Option Explicit tells the compiler to throw an error when a variable is not declared. It could be that you're using lot__no
and the variable is actually named lot_no
.
This works as expected, so the problem it is not in your posted code.
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
lot__no = "Lot no>928374"
then I would do
if s.indexof(lot__no) >= 0 then
'do stuff
end if
but since you seem to play with xml, you should use the build-in .net xml parser
don't build your own one
If you are using long strings you should use regular expressions instead, they are faster:
If Regex.IsMatch(s, lot__no) Then ...
If the strings are short, the s.Contains is fine.
精彩评论