[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SPlit string after =

Posted on 2012-08-30
4
Medium Priority
?
726 Views
Last Modified: 2012-08-30
I need to get the string from a larger string

In my source string will be a "starter" string PP=  I need everything after that and before the very next comma

So...if my source string is

GR-4,DD=3,SW=N,PP=lrbrister,PE=1234

Then I just want
lrbrister

also...the PP= can appear anywhere in the string
0
Comment
Question by:lrbrister
  • 2
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38352153
Using standard string manipulation functions:
        Dim data As String = "GR-4,DD=3,SW=N,PP=lrbrister,PE=1234"

        Dim startTag As String = "PP="
        Dim startIndex As Integer = data.IndexOf(startTag)
        If startIndex <> -1 Then
            Dim commaIndex As Integer = data.IndexOf(",", startIndex)
            If commaIndex <> -1 Then
                Dim value As String = data.Substring(startIndex + startTag.Length, commaIndex - (startIndex + startTag.Length))
                Debug.Print("value = " & value)
            End If
        End If

Open in new window

0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1600 total points
ID: 38352168
*You stated:

    "I need everything after that and before the very next comma"

So the above code assumes there will be a comma after that value, which might not be the case if it appears at the end of the string?

If this could be a possibility, then do:
        Dim data As String = "GR-4,DD=3,SW=N,PP=lrbrister,PE=1234"

        Dim startTag As String = "PP="
        Dim startIndex As Integer = data.IndexOf(startTag)
        If startIndex <> -1 Then
            Dim value As String
            Dim commaIndex As Integer = data.IndexOf(",", startIndex)
            If commaIndex <> -1 Then
                 value = data.Substring(startIndex + startTag.Length, commaIndex - (startIndex + startTag.Length))
            Else
                value = data.Substring(startIndex + startTag.Length)
            End If
            Debug.Print("value = " & value)
        End If

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 38352218
So I'll make it "Opposite Day" and take the Split approach where Idle_Mind has done the Substring approach  : )

Module Module1

    Sub Main()
        Dim input As String = "GR-4,DD=3,SW=N"
        Dim identifier As String = "PP"
        Dim result As String = GetValueForKey(input, identifier)

        Console.WriteLine(result)

    End Sub

    Function GetValueForKey(ByVal source As String, ByVal key As String) As String
        Dim parts() As String = source.Split("="c)

        For i As Integer = 0 To parts.Length - 1
            If parts(i).EndsWith(key) AndAlso i < parts.Length - 1 Then
                Dim subparts() As String = parts(i + 1).Split(","c)

                Return subparts(0)
            End If
        Next

        Return source
    End Function

End Module

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 38352279
Perfect guys.

A nod to kaufmed for an alternate solution
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

826 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