none
How to parse a string and find a specific text ? RRS feed

  • Question

  • I have this code:

    private void videoCardType()
            {
                string graphicsCard = string.Empty;
                foreach (ManagementObject mo in searcher.Get())
                {
                    foreach (PropertyData property in mo.Properties)
                    {
                        if (property.Name == "Description")
                        {
                            graphicsCard = property.Value.ToString();
                        }
                    }
                }
    
                string toPring = "NVIDIa";
                foreach (Match m in Regex.Matches(graphicsCard, toPring, RegexOptions.IgnoreCase))
                
                label4.Text = toPring;
            }

    Now in this case graphicsCard wontain "NVIDIA.....gtx...."

    I want to get only the "NVIDIA" from the graphicsCard variable.

    If im using Regex in this case i can put in the toPring variable the text i want to find in any case for example: "NVidiA" or "NVIDIA" or "nvIdIA"

    In all cases it will get the "NVIDIA" from the graphicsCard variable but the label4 Text style/case will be according to what i put in the toPring variable in the first place.

    If i put it as it is now so label4.Text will be "NVIDIa" and if i iwll put there "NVIDIA" so label4 text will be "NVIDIA".

    What i want is that in the toPring variable i will be able to type put the text im searching for in any case i want if upper case or lower case or part of it lower or upper and it will find the text and show me it in label4 as it was original in the graphicsCard variable.

    So if in graphicsCard nvidia is "NVIDIA" so i want to make that not matter how i type "NVIDIA" in the toPrint variable it will allways be in the end "NVIDIA"

    And if in graphicsCard nvidia was like "nvidia" so even if i type in toPring "NVIDIA" everything capital letters in label4 it will show as "nvidia" the result in label4 will be according to how it was in graphicsCard.


    danieli

    Friday, August 3, 2012 4:22 AM

Answers

  • You can try like:

                string graphicsCard = "...nvIDia.....gtx...."; //maybe there are even some chars infront nvidia keyword
                string toPring = "NVIDIa";
                if (graphicsCard.ToUpper().Contains(toPring.ToUpper()))
                {
                    int startingIndex = graphicsCard.ToUpper().IndexOf(toPring.ToUpper());
                    graphicsCard = graphicsCard.Substring(startingIndex, toPring.Length); //graphicsCard now has your desired value
                }     


    Mitja

    • Marked as answer by chocolade Friday, August 3, 2012 6:01 AM
    Friday, August 3, 2012 4:43 AM