2009-10-28 16 views
0

lot__no的位置怪異的行爲是一種公共字符串vb.net:與得到的字符串

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

'lot__no'包含什麼? – 2009-10-28 19:11:49

+1

變量temp_string保存的是什麼?變量lot__沒有保存什麼? – 2009-10-28 19:12:36

+0

它只是一些字符串像「lot238」 – 2009-10-28 19:12:49

回答

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

lot__no =「批號> 928374」

然後我會做

if s.indexof(lot__no) >= 0 then 
    'do stuff 
end if 

,但因爲你似乎與XML玩,你應該使用build項。淨XML解析器

不建立自己的一個

+0

應該是:> -1 – 2009-10-28 19:21:35

+0

@Jan,糟糕...漫長的一天:-) – Fredou 2009-10-28 19:23:01

+0

不工作抱歉 – 2009-10-28 19:24:19

2

由於您沒有Option Explicit開啓 - 這樣做。你的代碼在我的機器上工作,所以這是我能看到的唯一一個問題。

Option Explicit告知編譯器在未聲明變量時拋出錯誤。可能是因爲您使用的是lot__no,而該變量實際上名稱爲lot_no

+0

我用選項顯式,沒有做任何事情 – 2009-10-28 21:38:10

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 
0

如果您使用的長字符串,你應該使用正則表達式來代替,他們更快:

If Regex.IsMatch(s, lot__no) Then ... 

如果字符串總之,s.Contains是好的。