Solved

vb.net containts to check certain words

Posted on 2014-11-26
10
113 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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