Solved

Parse a string and return a string.

Posted on 2008-06-25
7
222 Views
Last Modified: 2013-12-16
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
Comment
Question by:mathieu_cupryk
7 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 21867118
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
 
LVL 3

Expert Comment

by:GHCS_Mark
ID: 21867129
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
 
LVL 16

Expert Comment

by:CuteBug
ID: 21867128
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:mathieu_cupryk
ID: 21867188
does not work?
0
 
LVL 27

Expert Comment

by:planocz
ID: 21867212
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
 

Author Comment

by:mathieu_cupryk
ID: 21867241
should it be == 1?
0
 
LVL 16

Accepted Solution

by:
CuteBug earned 500 total points
ID: 21867506
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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