Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parse a string and return a string.

Posted on 2008-06-25
7
Medium Priority
?
234 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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