Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find word in string 2

Status
Not open for further replies.

MDA

Technical User
Joined
Jan 16, 2001
Messages
243
Location
US

Hi all,

I am trying to pull out part of a string but am having problems figuring out how to do it with vb .net. The string comes from a record in a database.

The string looks like:
connectstring='Data Source=TESTSERVER;Initial Catalog=TESTDB;uid=sa;pwd=sa;'

I need to extract the uid and the pwd into two text boxes. I have code to find where for instance uid starts but can't figure out how to get the 'sa'

I assume I need a way to specify both a start and stop point and then get the text. This is what I have so far but it is not working.

Dim uid As String = lvConnection.SelectedItems(0).SubItems(1).Text
Dim uidEnd As String = lvConnection.SelectedItems(0).SubItems(1).Text
Dim uidtext As String
uid = uid.IndexOf("uid=") + 4
uidEnd = uidEnd.IndexOf(";pwd")
uidtext = lvConnection.SelectedItems(0).SubItems(1).Text.Substring(uid,2)


Any ideas are greatly appreciated..

Cheers and Happy New Year.

MDA
 
This function will parse it out for you

'Add this to your form load or where ever.

'Dim connectstring As String = "Data Source=TESTSERVER;Initial Catalog=TESTDB;uid=sa;pwd=sa;"

textbox1.text = ParseFields(connectstring, "uid=")

textbox2.text = ParseFields(connectstring, "pwd=")




'Add this function to your class

Function ParseFields(ByVal body As String, ByVal FieldName As String) As String
If body.IndexOf(FieldName) <> -1 Then
Dim fStart As Integer = body.IndexOf(FieldName) + FieldName.ToString.Length ' start of field 1
Dim fEnd As Integer = body.IndexOf(&quot;;&quot;, fStart) ' end of field 1
Dim fld As String = Trim(body.Substring((fStart), (fEnd - fStart)))
Return fld
Else
Return &quot;&quot;
End If
End Function

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
You can also use the Split method on the String object:
Code:
Dim connectString = &quot;Data Source=TESTSERVER;Initial Catalog=TESTDB;uid=sa;pwd=sa;&quot;

Dim splitString = As String() = connectString.Split(New [Char]() {&quot;;&quot;c})
which gives you an array of strings that looks like:
[tt]
Element 1: &quot;Data Source=TESTSERVER&quot;
Element 2: &quot;Initial Catalog=TESTDB&quot;
Element 3: &quot;uid=sa&quot;
Element 4: &quot;pwd=sa&quot;
[/tt]
You can then repeat the process for the parts of the connect string you're interested in:
Code:
Dim connectString = &quot;Data Source=TESTSERVER;Initial Catalog=TESTDB;uid=sa;pwd=sa;&quot;

Dim splitString = As String() = connectString.Split(New [Char]() {&quot;;&quot;c})
Dim connectPart = As String() = splitString(3).Split(New [Char]() {&quot;=&quot;c})
Dim uid As String = connectPart(2)
connectPart = splitString(4).Split(New [Char]() {&quot;=&quot;c})
Dim pwd As String = connectPart(2)
The &quot;=&quot;c is an interesting part of the VB.NET language. It converts the single-character string &quot;=&quot; into a single-character long datatype, suitable for assigning to a variable of type Char.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top