Solved

vb.net containts to check certain words

Posted on 2014-11-26
10
111 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
  • 5
  • 4
10 Comments
 
LVL 49

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 33

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 33

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 33

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 33

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

805 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