none
How can I substring from a file content specific words ? RRS feed

  • Question

  • private List<string> GetResults(string file)
            {
                List<string> results = new List<string>();
    
                string word = textBox1.Text;
                string[] words = word.Split(new string[] { ",," }, StringSplitOptions.None);
    
                List<int> alli = new List<int>();
    
                for(int i = 0; i < words.Length; i++)
                {
                    alli = AllIndexesOf(file, words[i]);
                }
    
                for(int i = 0; i < valuesLengths.Count; i++)
                {
                    results.Add(file.Substring(alli[i], valuesLengths[i]));
                }
    
                return results;
            }
    
            List<int> indexes = new List<int>();
            List<int> valuesLengths = new List<int>();
            public List<int> AllIndexesOf(string str, string value)
            {
                if (String.IsNullOrEmpty(value))
                    throw new ArgumentException("the string to find may not be empty", "value");
                
                for (int index = 0; ; index += value.Length)
                {
                    index = str.IndexOf(value, index, StringComparison.CurrentCultureIgnoreCase);
                    if (index == -1)
                        return indexes;
                    indexes.Add(index);
                    valuesLengths.Add(value.Length);
                }
            }

    In the first file it seems to be fine I'm making a loop of some files with the GetResults.

    In the second file the results incorrect at some indexs. It find other words or not the right length.

    The first screenshot showing the words to be find :

    The second screenshot showing some of the file content :

    The last screenshot showing the results of this file :

    For example indexs 1,4,5 why it's substring this strings ? It seems like the length values are wrong for this indexs or maybe something else is wrong in the AllIndexesOf function ? Not sure where is the problem and how to fix it. I know I can use other ways but I want to know what is wrong in this case using indexof and substring.

    Wednesday, May 27, 2020 12:26 AM

Answers

  • Hi Chocolade1972,
    I made a test with your code, it works fine.
    In my test, I use ";" to split string.
    Here is my file:  

    string file = "using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;";

    And I only change this line:

    string[] words = word.Split(new string[] { ",," }, StringSplitOptions.None);

    to

    string[] words = word.Split(new string[] { ";" }, StringSplitOptions.None);

    So, you need to check that if using ",," is applicable to other files.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 27, 2020 5:40 AM