?
Solved

vb.net containts to check certain words

Posted on 2014-11-26
10
Medium Priority
?
123 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 52

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 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