Solved

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

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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