Solved

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

Posted on 2009-07-10
1
234 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
[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
1 Comment
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 125 total points
ID: 24825538
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

Industry Leaders: 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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 …
Suggested Courses

736 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