Solved

vb.net containts to check certain words

Posted on 2014-11-26
10
114 Views
Last Modified: 2014-11-26
Hello Experts
I am trying to check a list of words and see if its available in the text (value)

Dim NonChangeable() As String = {"Daddy", "dad"}
Dim TagsToCheck() as String = {"<Father>","<Grandfather>","<GreatGrandFather>" }
Dim value As String = Trim(item.Value.Replace(Environment.NewLine, " \line "))

If text(i).Contains(TagsToCheck) Then
         If value.ToLower.Contains(NonChangeable) Then                            
             format(i) = "{0}"
          End If
End if

This is not working. Please can someone advice?

Thanks
M
0
Comment
Question by:mehmast
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40466727
Hi,

As far as I know

.Contains

 works with a string not an array of strings you should loop through them

Regards
0
 

Author Comment

by:mehmast
ID: 40466807
Hi thanks for your input, is there any other way to use Contains without iterating in the loop?
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40466811
I think I know what you are after but need a little more information.  What do the variables text, value and format contain?

-saige-
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:it_saige
ID: 40466818
Nevermind about value, what about item?

-saige-
0
 

Author Comment

by:mehmast
ID: 40466849
hello saige
thanks for looking into this for me

text(i) = "<Father>"
item.value ="daddy"

thanks
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40466894
Well here is what I come up with:
Module Module1
	Private NonChangeable As String() = {"Daddy", "dad"}
	Private TagsToCheck As String() = {"<Father>", "<Grandfather>", "<GreatGrandFather>"}

	Sub Main()
		Dim text As String = "<Father>"
		Dim value As String = "daddy"
		Dim format As String = "I should change."

		Console.WriteLine(String.Format("Before linq: format = {0}", format))
		If Not String.IsNullOrEmpty((From tag As String In TagsToCheck Where text.Contains(tag) Select tag).FirstOrDefault()) Then
			If Not String.IsNullOrEmpty((From [static] As String In NonChangeable Where value.ToLower().Contains([static].ToLower()) Select [static]).FirstOrDefault()) Then
				format = "{0}"
			End If
		End If
		Console.WriteLine(String.Format("After linq: format = {0}", format))
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following output:Capture.JPG
And even though I have gussied it up with linq, it is, still looping.

However, this does beg the question, what is your ultimate goal.  This as it stands is a real cumbersome way to go about string replacement and formatting.

-saige-
0
 

Author Comment

by:mehmast
ID: 40466904
I have tried looping through the code earlier but then regardless of whether I need to perform this validation, its checking for every item, I need to only check if the
text(i)="<Daddy>" then I want to check its value if its "daddy" or "Daddy" , if found set to {0}

May be I need to do in some other way ....  I hope you understand what I am trying to ask?

 Dim iPatterns As New List(Of String)()
            iPatterns.Add("<Father>")
            iPatterns.Add("<grandmother>")
            iPatterns.Add("<grandmother's>")
            iPatterns.Add("<thegrandmother>")
            iPatterns.Add("<thegrandmother's>")

            Dim iPatternValue As New List(Of String)()
            iPatternValue.Add("daddy")
            iPatternValue.Add("granny")
            iPatternValue.Add("grandma")
            iPatternValue.Add("dad")



 For Each key As String In iPatterns
                            If text(i).ToLower.Contains(key) Then
                                For Each val As String In iPatternValue
                                    If value.ToLower.Contains(val) Then                                                               format(i) = "{0}"
                                    End If
                                Next
                            End If
                        Next

Open in new window

0
 

Author Comment

by:mehmast
ID: 40466908
Thanks Saige, I can't use Linq am afraid, still on  Net 2.0

Typo above**

text(i)="<Daddy>"  

This should be

text(i)="<Father>"
0
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40467017
How about this:
Module Module1
	Sub Main()
		Dim iPatterns As New List(Of String)()
		iPatterns.Add("<Father>")
		iPatterns.Add("<grandmother>")
		iPatterns.Add("<grandmother's>")
		iPatterns.Add("<thegrandmother>")
		iPatterns.Add("<thegrandmother's>")

		Dim iPatternValue As New List(Of String)()
		iPatternValue.Add("daddy")
		iPatternValue.Add("granny")
		iPatternValue.Add("grandma")
		iPatternValue.Add("dad")

		Dim text As String = "<Father>"
		Dim value As String = "daddy"
		Dim format As String = "I should change."


		Console.WriteLine(String.Format("Before loop: format = {0}", format))
		For Each key As String In iPatterns
			If text.ToLower().Contains(key.ToLower()) Then
				For Each val As String In iPatternValue
					If value.ToLower().Contains(val.ToLower()) Then
						format = "{0}"
						Exit For
					End If
				Next
			End If
		Next
		Console.WriteLine(String.Format("After loop: format = {0}", format))
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following output:Capture.JPG
-saige-
0
 

Author Closing Comment

by:mehmast
ID: 40467234
thank you, I will use this solution instead
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question