Solved

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

Posted on 2009-07-10
1
233 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
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 54
Expression Evaluater 3 45
Where is this file? 3 32
Nested forach loop to linq 3 24
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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 …

685 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