开发者

get all combination of consecutive elements of string array while keeping the remaining elements C#

i'm new in programming and i have a difficult time with the following task.

I have a string let's say string final_word="Bacliff New Texas United States"; and i want to do the following:

1st check for the entire string in a databases, if a match ex开发者_如何学Goists i'm ok if not I want to search for the substring "Bacliff New Texas United".

If a match is found i'll search for the substring "States", if not I'll search for the substring "New Texas United States", if a match is found search for the substring "Bacliff".

If not, search for the substring "Bacliff New Texas".if a match is found search for the substring "United States" and if no matches are found for the last substring search for the substrings "United"and "States".

Else search for the substring "New Texas United", now if a match is found i will search the database for two individual substrings "Bacliff" and "States", because i want all the substrings to be sequential words in the original string. and i will keep looking in the same way. If no matches found for each combination of two words, i will search each word individual.

In short,

First i have to search for the entire string

Second, search for substring with 5 words but keep the others in case a match is found for the first substring.

Third, search for sunstrings with 4 consecutive words and keep the others with respect to the original position.

Next search for substrings with 3 consequtive words etc..

At last search for each individual word.

if at some point a match is found for any combination of words, i will store this word and keep looking in the same ways for the other combinations..

I hope i made my point clear.. any help would be truly appreciated....

Thank you in advance.........


You can split your input string into an array, then re-join the sub-arrays of consecutive words:

using System;

class Program {
    static void Main(string[] args) {
        string full = "Bacliff New Texas United States";
        // split the string in words
        string[] words = full.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        // get substrings 'size' words long
        for (int size = 1; size <= words.Length; ++size) {
            string[] destination = new string[size];
            for (int start = 0; start <= words.Length - size; ++start) {
                Array.Copy(words, start, destination, 0, size);
                Console.WriteLine(string.Join(" ", destination));
            }
        }
    }
}

EDIT:

Sorry, I'm not 100% sure if I understood correctly what you want :)
Is this more like it?

using System;

class Program {
    static void Main(string[] args) {
        string full = "Bacliff New Texas United States";
        // split the string in words
        string[] words = full.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        for (int size = 1; size < words.Length; ++size) {
            // get substrings of 'size' words
            for (int start = 0; start <= words.Length - size; ++start) {
                string[] before = new string[start];
                string[] destination = new string[size];
                string[] after = new string[words.Length - (size + start)];
                Array.Copy(words, 0, before, 0, before.Length);
                Array.Copy(words, start, destination, 0, destination.Length);
                Array.Copy(words, start + destination.Length, after, 0, after.Length);
                Console.WriteLine("{0} % {1} % {2}",
                    string.Join(" ", before),
                    string.Join(" ", destination),
                    string.Join(" ", after));
            }
        }
    }
}


if you are using Ms SQL then better use Full Text Search

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜