Solved

enumerating certain elements in a string array

Posted on 2011-09-21
12
150 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

14 Experts available now in Live!

Get 1:1 Help Now