Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Searching a string for multiple words using C#

Posted on 2004-10-25
12
Medium Priority
?
399 Views
Last Modified: 2008-01-16
This is what I need to do. I wrote it in Psudocode

Examples of string coming from client computer:
string1: "Disk "ST380013AS (Ch2)" failed
String2: "Rebuilding successfully completed for disk "ST380013AS (Ch2)"

Function for the strings: Server side

      private void ReceiveData(Byte [] byteBuffer, int size)
      {
         string sData = Encoding.ASCII.GetString(byteBuffer,0, size);

            if (sData) contains words "Disk" and "failed" (either lower or uppercase)
              then
                      //pop up the screen for the user
                      open up messagebox or form that contains "Disk Failure" This form or messagebox must stay on
                      top of all applications (focus) until the user closes it.
                 ***Need to do try catch here

            else if (sData) contains the words "Disk", "Rebuilding" and "Successfully" (either lower or uppercase)
              then
                       //Post the message into the errorlog
                System.Diagnostics.EventLog.WriteEntry(this.ToString(),"OCSEventListener" + sData);
               ***Need to do try catch here


Questions need answered
Can I use 1 function to do both strings or do I need 2 functions?
Does anyone have an example on performing the search of the string for the mulitple words?
0
Comment
Question by:NewMom2Brandon
[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
  • 6
  • 6
12 Comments
 
LVL 3

Expert Comment

by:HendrikTYR
ID: 12403361
       private void ReceiveData(Byte [] byteBuffer, int size) {
            string sData = Encoding.ASCII.GetString(byteBuffer,0, size);

            if((sData.ToLower().IndexOf("disk") > 0) &&
                (sData.ToLower().IndexOf("failed") > 0)) {
                // do your message box stuff here
            }
            else if((sData.ToLower().IndexOf("disk") > 0) &&
                (sData.ToLower().IndexOf("rebuilding") > 0) &&
                (sData.ToLower().IndexOf("successfully") > 0)) {
                // do your errorlog stuff here
            }
        }
0
 
LVL 3

Accepted Solution

by:
HendrikTYR earned 2000 total points
ID: 12403700
Sorry, make that:

private void ReceiveData(Byte [] byteBuffer, int size) {
            string sData = Encoding.ASCII.GetString(byteBuffer,0, size);

            if((sData.ToLower().IndexOf("disk") > -1) &&
                (sData.ToLower().IndexOf("failed") > -1)) {
                // do your message box stuff here
            }
            else if((sData.ToLower().IndexOf("disk") > -1) &&
                (sData.ToLower().IndexOf("rebuilding") > -1) &&
                (sData.ToLower().IndexOf("successfully") > -1)) {
                // do your errorlog stuff here
            }
        }

.... the MSDN documentation on this is actually incorrect....
in your second string IndexOf("Rebuilding") returns zero, which is the index of that word, but according to the MSDN docs on IndexOf(string value):
Return Value
Value                                        Meaning
A positive index position.             value was found.
0                                              value is Empty.          (this is incorrect)
-1                                             value was not found.

0
 
LVL 2

Author Comment

by:NewMom2Brandon
ID: 12404021
I am looking to make sure the word is in the string so would it still be -1 or would it be 1
I want the messagebox to pop up when the two value disk and failed are in the string. same for the other the values but instead of the message box it just writes into the error log.
0
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!

 
LVL 2

Author Comment

by:NewMom2Brandon
ID: 12404085
ok scratch my last comment...thank you for you help
0
 
LVL 3

Expert Comment

by:HendrikTYR
ID: 12404089
Yip, use the code from my second posting:  If IndexOf(...) returns a value higher than -1 it means the word was found.

Do you need help with the message box and errorlog stuff as well?
0
 
LVL 2

Author Comment

by:NewMom2Brandon
ID: 12404220
I have the information for the errorlog stuff. I belive it is just

System.Diagnostics.EventLog.WriteEntry(this.ToString(),sData);

I am a little stuck on how to make the messagebox stay up on the screen. I have another application that is running and this is to pop up from outside the application so it needs to stay on top of everything until the user clicks it. Do you have any suggestions on that
0
 
LVL 3

Expert Comment

by:HendrikTYR
ID: 12404480
Try this:

MessageBox.Show("Your warning message", "WARNING!!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
0
 
LVL 2

Author Comment

by:NewMom2Brandon
ID: 12404594
I saw this under the foxpro. Do I need to include the 4096 into the messagebox example you provided

You can make sure your messagebox stays on top of all other applications by using

messagebox("Error!",4096,"System Message")

the 4096 makes the messagebox system modal.  You can add this to the standard messagebox parameters for example

messagebox("Error!",4096+36,"System Message")

will display the 'Error!' message with a Yes and No dialogue box.
0
 
LVL 3

Expert Comment

by:HendrikTYR
ID: 12404661
not needed...

MessageBoxOptions.ServiceNotification will keep it on top, even when nobody is logged on.
0
 
LVL 2

Author Comment

by:NewMom2Brandon
ID: 12404681
ahh thank you so much for your help!! You have saved me hours of digging!!
0
 
LVL 2

Author Comment

by:NewMom2Brandon
ID: 12404712
One more question is there a way to make the messagebox be full screen or at least large enough to be an annoyance?
0
 
LVL 3

Expert Comment

by:HendrikTYR
ID: 12404774
Tough one...
If you put in a very long message...yes
Quick and dirty solution:  
string errMsg = "Whatever your message is".PadLeft(300, ' ').PadRight(300, ' ');
...hehe
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

618 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