Solved

vb.net containts to check certain words

Posted on 2014-11-26
10
103 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 48

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 32

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
 
LVL 32

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

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 32

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now