Solved

picking different combination of words from an array of words

Posted on 2011-09-21
4
154 Views
Last Modified: 2012-05-12
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
Comment
Question by:TrialUser
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36578344
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
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 36579474
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
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 500 total points
ID: 36580179
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36582262
You have asked the same question 3 times now!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now