Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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);
0
mathieu_cupryk
Asked:
mathieu_cupryk
1 Solution
 
CuteBugCommented:
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";
0
 
GHCS_MarkCommented:
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

0
 
CuteBugCommented:
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";
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mathieu_cuprykAuthor Commented:
does not work?
0
 
planoczCommented:
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
0
 
mathieu_cuprykAuthor Commented:
should it be == 1?
0
 
CuteBugCommented:
No it should not be ==1

There was a typo... there is a space between the words "Data" and "Source". That is why the search was failing.

So here is the correct updated code

 
string strToParse="Data Source=SCWB2.wpg.cwb.ca;Persist Security Info=True;User ID=init_price;Password=init_77day;Unicode=True";
 
if (strToParse.IndexOf("Data Source=SCWB2") != -1)
    return "System Integration Testing";
else if  (strToParse.IndexOf("Data Source=UCWB2") != -1)
     return "Client Acceptance Testing";

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now