Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

Parse a string and return a string.

I got the following string:
"Data Source=SCWB2.wpg.cwb.ca;Persist Security Info=True;User ID=init_price;Password=init_77day;Unicode=True"

I need to return get the first SCWB2

if SCWB2 then return ("System Integration Testing");
else if UCWB2 then return (Client Acceptance Testing);
Avatar of CuteBug
CuteBug
Flag of India image

string strToParse="Data Source=SCWB2.wpg.cwb.ca;Persist Security Info=True;User ID=init_price;Password=init_77day;Unicode=True";

if (strToParse.Find("DataSource=SCWB2") != -1)
    return "System Integration Testing";
else if  (strToParse.Find("DataSource=UCWB2") != -1)
     return "Client Acceptance Testing";
Simple terms, you can use the following, though it has no bounds checking, etc.

You could also change the string check and remove the third array usage, simply checking for "SCWB2.wpg.cwb.ca" and "UCWB2.wpg.cwb.ca" instead.
Dim strOrig as String = "Data Source=SCWB2.wpg.cwb.ca;Persist Security Info=True;User ID=init_price;Password=init_77day;Unicode=True"
Dim strArr1 as String() = strOrig.Split(";")
Dim strArr2 as String() = strArr1(0).Split("=")
Dim strArr3 as String() = strArr2(1).Split(".")
 
If strArr3(0).ToUpper() = "SCWB2" then return ("System Integration Testing")
else if strArr3(0).ToUpper() = "UCWB2" then return ("Client Acceptance Testing")

Open in new window

I'm sorry! You have to use IndexOf method instead of Find.

So the correct code looks like this

string strToParse="Data Source=SCWB2.wpg.cwb.ca;Persist Security Info=True;User ID=init_price;Password=init_77day;Unicode=True";

if (strToParse.IndexOf("DataSource=SCWB2") != -1)
    return "System Integration Testing";
else if  (strToParse.IndexOf("DataSource=UCWB2") != -1)
     return "Client Acceptance Testing";
Avatar of mathieu_cupryk

ASKER

does not work?
Avatar of Howard Cantrell
Try this saample....
just change the strings to your string inputs

Imports System.Text.RegularExpressions

  'SAMPLE : Matching("abc", "123abc456")
    Private Sub Matching(ByVal str As String, ByVal strInput As String)
        ' cCreate a new Regex object.
        Dim r As New Regex(str)
        ' Find a single match in the input string.
        '             Positions---012345678
        Dim m As Match = r.Match(strInput)
        If m.Success Then
            ' Print out the character position where a match was found.
            ' (Character position 3 in this case.)
            Console.WriteLine("Found match at position " & m.Index.ToString())
        End If
        '*** Output *******
        'Found match at position 3
    End Sub
should it be == 1?
ASKER CERTIFIED SOLUTION
Avatar of CuteBug
CuteBug
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial