Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

enumerating certain elements in a string array

Posted on 2011-09-21
12
Medium Priority
?
162 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 60

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 60

Accepted Solution

by:
Kevin Cross earned 1000 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 60

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
 
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 1000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

810 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