Solved

How to find string values based on a string with multiple possibilities

Posted on 2009-07-10
1
228 Views
Last Modified: 2012-05-07
Hi!

What i want to do is the folowing.

I have a string called currentCodeLine this line is filled with a code line read from a source (a 4G language)

I also have a string called declaredVariables, containing: "iused inotused"

now i want to check if one or more of the words in the declaredVariables is in the currentCodeLine string.

I have tried to use the indexof, contains etc, but probably it can only search for a single word at a time.

Regards
Martin
0
Comment
Question by:MrHswede
1 Comment
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 125 total points
Comment Utility
Try something like this, using Split to break your variables up then foreach syntax to search each once at a time.  Yes think your issue is that those variables probably don't appear together in sequence with a space so you won't get any matches unless you check independently.
string currentCodeLine = "iused x = some value that is inotused";

            string declaredVariables = "iused inotused";

            int count = 0;
 

            foreach (string s in declaredVariables.Split(" ".ToCharArray())) {

                if (currentCodeLine.Contains(s)) count++;

            }
 

            Console.WriteLine(String.Format("Found {0} instances!", count));

Open in new window

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

13 Experts available now in Live!

Get 1:1 Help Now