Solved

enumerating certain elements in a string array

Posted on 2011-09-21
12
151 Views
Last Modified: 2012-05-12
I have an array of n string. I need a way to extract all n-1, n-2, n-3...1 string from the n strings.

I would like to do this in vb.net or sql server 2008. please help thanks
0
Comment
Question by:TrialUser
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36578132
If this is an array, then you should be able to do this easily on the VB.NET side, correct? Therefore, I suspect you have a comma-delimited string. If so, then you can use a Split function.

In VB.NET, you have one built in - http://msdn.microsoft.com/en-us/library/6x627e5f(v=vs.80).aspx.
For SQL, you can create one http://www.experts-exchange.com/Q_23786715.html; additionally, see Brandon's article - http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/A_192-Delimited-String-Parsing-in-SQL-Server-2005-and-later.html.
0
 

Author Comment

by:TrialUser
ID: 36578209
I know how to split a string. My problem is getting n-1 strings, n-2 string and so on till just 1 stirng out of all strings in the array. please help thanks
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 36578225
Did you look at the links I sent? In particular, look at the SQL question here on EE. If you know how to split a string, then you can get at n-x strings easily. Look at my example of split with position. If you have that, you essentially have the index along side the split string. You can get to what ever is needed that way. Same in VB.NET code. When you split, you are getting back an array and so have index at your disposal. Maybe if you showed with a concrete example that would help.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36578261
If you are meaning to slowly pull apart the string, so that you go from:
A,B,C,D,E
A,B,C,D
A,B,C
A,B
A

Then you may want to look at http://msdn.microsoft.com/en-us/library/system.string.lastindexof.aspx. You basically get the LEFT() portion of the string up to the LastIndexOf(",") or whatever delimits the string.

You can do something similar in SQL.
DECLARE @str AS VARCHAR(10) = 'A,B,C,D,E';

WHILE (CHARINDEX(',', @str) > 0)
    BEGIN
    SELECT @str = LEFT(@str, LEN(@str)-CHARINDEX(',', REVERSE(@str)));
    PRINT(@str);
END

Open in new window


Kevin
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36579482
If you want to get a random selection from your string, then try this function. Give the function the number of items youwant returned from the string, and the string itself, e.g
Dim MyResult = getRandon(String.Join(",", {"apple", "pink", "lotus", "baby", "pen", "paper"}), 2)

Open in new window

Then the function
    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

0
 

Author Comment

by:TrialUser
ID: 36579853
Sorry, may be I did not explain properly. I have an array of say 5 words. I want to extract all acombinations of 4 words, 3 words, 2 words, 1 word from the array of 5 words.

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 17

Expert Comment

by:nepaluz
ID: 36579987
OK, I mis-understood. Here goes,
Dim theList = {"apple", "pink", "lotus", "baby", "pen", "paper"}
Dim Result As New Dictionary(Of Integer, List(Of String)) From {{3, Nothing}, {4, Nothing}, {5, Nothing}, {6, Nothing}, {7, Nothing}}
Array.ForEach(Result.Keys.ToArray, Sub(x)
                                       For Each q In theList
                                           If q.Length = x Then
                                               If Result.Item(x) Is Nothing Then
                                                   Result.Item(x) = New List(Of String) From {q}
                                               Else
                                                   Result.Item(x).Add(q)
                                               End If
                                           End If
                                       Next
                                   End Sub)

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36580011
You can then extract just the items with values using LINQ thus:
Dim FinalResult = From xd In Result Where xd.Value IsNot Nothing Select xd

Open in new window

There you have the length of the items as the key and the items in a listas thevalue of that key.
0
 

Author Comment

by:TrialUser
ID: 36580099
Could you please paste the entire code in a compilable format. Sorry I am not familiar with any of the commands you have used.

I am getting compile error right from : Array.ForEach(Result.Keys.ToArray, Sub(x)
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36580193
OK I have removed the code that gives you errors,and thisis the entire routine.
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 17

Assisted Solution

by:nepaluz
nepaluz earned 250 total points
ID: 36580202
code above had an error, here's the correct code.
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.Add(x.Length, New List(Of String) From {x})
    End If
Next

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36582257
Is this question relevant anymore given that you have asked another question about combinations?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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