• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

picking different combination of words from an array of words

I have a string for example : apple pink lotus baby pen paper red

i wan extract     6 words then 5 words 4 words 3 words 2 words then 1 word from this array.. for example: apple pink lotus baby pen paper
apple pink lotus baby pen red
apple pink lotus baby paper red

and so on....
apple pink lotus baby pen
...

apple
pink
lotus
baby pen

Please help thanks. If there is code in VB.net that will be great
0
TrialUser
Asked:
TrialUser
  • 2
2 Solutions
 
Easwaran ParamasivamCommented:
I've implemented to get 6 random strings. Likewise write for other cases. Refer: http://www.techrepublic.com/article/working-with-random-numbers-in-net-applications/5663283
using System;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;

namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            

            string[] myStrings = new string[7];
            myStrings = "apple pink lotus baby pen paper red".Split(" ".ToCharArray());


            Random objRandom = new Random();
            StringCollection objCollection = new StringCollection();

            //To get 6 values randomly
            for (int i = 0; i < 6; )
            {
                int random = objRandom.Next(0, 7); 

                if (!objCollection.Contains(myStrings[random]))
                {
                    objCollection.Add(myStrings[random]);
                    i++;
                }

            }

            foreach(string s in objCollection)
              Console.WriteLine(s);

            Console.Read();

        }
    }
}

Open in new window

0
 
nepaluzCommented:
here is a VB.NET function that will return the number of items that you require from a given string.
    Function getRandon(ByVal items As String, ByVal number As Integer) As String

        Dim objRandom As New Random
        Dim objCollection As New List(Of String)
        Dim objReturn As New List(Of String)
        Array.ForEach(items.Split(","), Sub(x) objCollection.Add(x))
        While objReturn.Count < number
            Dim random = objRandom.Next(0, number + 1)
            If Not objReturn.Contains(objCollection.ToList(random)) Then
                objReturn.Add(objCollection.ToList(random))
            End If
        End While
        Return String.Join(",", objReturn)
    End Function

Open in new window

call the function by:
Dim MyResult = getRandon(String.Join(",", {"apple", "pink", "lotus", "baby", "pen", "paper"}), 2)

Open in new window

0
 
nepaluzCommented:
For results into a blank dictonary, do
Dim theList = {"apple", "pink", "lotus", "baby", "pen", "paper"}
Dim Result As New Dictionary(Of Integer, List(Of String))
For Each x In theList
    If Result.ContainsKey(x.Length) Then
        Result.Item(x.Length).Add(x)
    Else
        Result.Item(x.Length) = New List(Of String) From {x}
    End If
Next

Open in new window

0
 
CodeCruiserCommented:
You have asked the same question 3 times now!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now