Solved

Searching a string for multiple words using C#

Posted on 2004-10-25
391 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
Question by:NewMom2Brandon
    12 Comments
     
    LVL 3

    Expert Comment

    by:HendrikTYR
           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:
    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
    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
     
    LVL 2

    Author Comment

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

    Expert Comment

    by:HendrikTYR
    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
    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
    Try this:

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

    Author Comment

    by:NewMom2Brandon
    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
    not needed...

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

    Author Comment

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

    Author Comment

    by:NewMom2Brandon
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    846 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

    10 Experts available now in Live!

    Get 1:1 Help Now