Iterate through string from end to start one word at at time

Viewed 130

I am looking how to iterate trough a string with word, starting at the end and going to the start, word by word.

The output should be like:

  • word1 word2 word3 word4
  • word1 word2 word3
  • word1 word2
  • word1

I don't really now where/how to start. The other way around I got working, but it turned out that's not what I need (see below).

string descriptionText = "Column Grid Building"; //example
string[] description1TextArray = descriptionText.Split(' ');
int noItems = description1TextArray.Count();

for (int i = 0; i < noItems; i++)
{
    if (i == 0)
    {
        search = search + description1TextArray[i];
    }
    else
    {
        search = search + " " + description1TextArray[i];
    }

   foreach (DataRow row in dt.Rows)
    {
        description1 = row[0].ToString(); 
        abbreviation1 = row[1].ToString();

        if (description1 == search || abbreviation1 == search)
        {
            comboBoxDescription1.SelectedIndex = comboBoxDescription1.FindStringExact(description1);
        }
    }
}

EDIT:

Sorry for any confusion.

I start with a string "word1 word2 word3 word4". I already have each word set to an array.

string[] description1TextArray = descriptionText.Split(' ');
int noItems = description1TextArray.Count();

I need to check a string against a value from a datatable. For example the value in dataTable is "Column Grid". My input is "Column Grid Building".

The reason I need to check from end to start is because there are values in the data table that overlap, like "Column Grid" and "Columnn". When I do the check on the string from start to end, the value "Column Grid" is never found because a match is already found on "Column".

I hope this explains it better.

3 Answers

You can try this:

using System.Linq;

string myString = "word1 word2 word3 word4";

var words = myString.Split(' ');

for ( int index = words.Length; index > 0 ; index-- )
{
  string sentence = string.Join(" ", words.Take(index));
  Console.WriteLine(sentence);
}

It is a loop which takes the required number of words by counting down the indexer.

Output

word1 word2 word3 word4
word1 word2 word3
word1 word2
word1

Try this way

string data = "word1 word2 word3 word4";
var strarray = data.Split(' ');
for ( int i = strarray.Length - 1; i >= 0; i-- )
{
  Console.WriteLine(strarray[i]);
}

The question is a bit confusing even after the Edit... If I understood correctly your problem is that you have some similar string in your database, for example you got:

1)"Column"

2)"Column grid"

3)"Column grid hello"

You recive as input the string "Column grid hello world" and you need to test this string VS all 3 items stored in your database. In theory all 3 strings match your input string, but you want to know wich one is the most similar? For example in this case the 3rd one "column grind hello".

Is that right?

If this is the problem is not easy as you may think, because is called Fuzzy (or Approximate) string matching.

Anyway if you are 100% sure that your input string will always be a string in your db + something else, you can try to do a different thing, because I don't see the point of splitting into an array of words. Can't you just use the .Contains() method?

For example:

string inputString; // user input string
List<string> yourOrderedDbCollection; // dunno if you are using sql or linq, anyway just order by lenght ascending
string foundItem; // the final found item

foreach (string yourDbString in yourOrderedDbCollection) 
{
   if(inputString.Contains(yourDbString))
   {
    foundItem = yourDbString;
    break;
   }
}
Related