Solved

Parse a string and return a string.

Posted on 2008-06-25
7
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

626 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