views:

52

answers:

1

Hi all, I try explain my issue:

note 1: I have only strings, not files, ONLY strings.

I have a string like this (NOTE: I include line numbers for better explain)

The line separator is \r\n (CRLF)

string allText = 

1 Lorem ipsum Lorem ipsum

2 == START 001partXXX.sql ==

3 Lorem ipsum TEXT Lorem ipsum

4 == END 001partXXX.sql ==

5 Lorem ipsum TEXT Lorem ipsum

6 == START 002partzzz.sql ==

7 Lorem ipsum TEXT Lorem ipsum

8 == END 002partzzz.sql ==

I have contents strings like this:

string contents1 =
== START 001partXXX.sql ==

Lorem ipsum TEXT Lorem ipsum

== END 001partXXX.sql ==

the other content string:

string contents2 =

== START 002partzzz.sql ==

Lorem ipsum TEXT Lorem ipsum

== END 002partzzz.sql ==

Then,

allText.IndexOf(contents1) != -1

allText.IndexOf(contents2) != -1

I need function thats receive 3 parameters: allText, Contents, and text to find in contents, and it returns the line number of Text To Find in AllText

For example,

input: allText, contents2, "TEXT"

ouput = line number 7

Another sample,

input: allText, contents1, "TEXT"

ouput = line number 3

Another sample,

input: allText, contents1, "TEXT NOT FOUND"

ouput = line number -1

How can I implement this function ?? any help very useful for me,

Thanks in advanced.

+1  A: 

Your question is quite confusing, but I think I got the gist of it. I would edit the question, but I don't have the rep to do so.

To recap, you have a string that contains the contents of multiple files. You want to know the index of a particular bit of text from one of the individual files in the larger master file.

This will return -1 when the text isn't found. I also assume the lines in your strings are separated by whatever the platform default is (\r\n on windows).

Hope this answers your question.

using System;
using System.Collections.Generic;
using System.IO;

public class MyClass
{
    public static void Main()
    {
        //read relevant text from disk
        string AllText = File.ReadAllText(@"c:\alltext.txt"); 
        string Contents = File.ReadAllText(@"c:\contents.txt");
        string text = "TEXT";

        int line = SearchForLine(AllText, Contents, text);

        Console.WriteLine(line);
        Console.Write("Press any key to continue...");
        Console.ReadKey();
    }

    static int SearchForLine(string AllText, string Contents, string SearchText)
    {
        //split strings into arrays based on newline character 
        //uses environment.newline (\r\n on windows). change to what you 
        //need if this isn't correct
        string[] AllTextSplit = SplitStringOnNewLine(AllText);
        string[] ContentsSplit = SplitStringOnNewLine(Contents);

        //find the first line of Contents in AllText 
        int start = FindIndex(AllTextSplit, ContentsSplit[0]);
        //find the last line of Contents in AllText 
        int stop = FindIndex(AllTextSplit, ContentsSplit[ContentsSplit.Length - 1]);

        //search alltext for SearchText between start and stop (lines where contents exist in AllText
        for (int i = start; i <= stop; i++)
        {
            if (AllTextSplit[i].IndexOf(SearchText)!=-1)
            {
                return i + 1; // + 1 because you want line numbers starting at 1   
            }
        }
       return -1; 
    }

    private static int FindIndex(string[] TextToSearch, string SearchText)
    {
        for (int i = 0; i < TextToSearch.Length; i++)
        {
            if (TextToSearch[i] == SearchText)
            {
                return i;
            }
        }
        return -1;
    }
    private static string[] SplitStringOnNewLine(string StringToSplit)
    {
        return StringToSplit.Split(new string[] { Environment.NewLine },
                       StringSplitOptions.None);
    }
}
Tim Coker