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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to split string into smaller variables at every "|" (pipe)

Status
Not open for further replies.

stre1026

IS-IT--Management
Jul 9, 2001
40
US
Hello,

I am coming back to VB after about 5 years of a break. I had gone off to learn PHP and forgot all of my VB in the process! I have been asked to design an application that will take a string of text separated by pipes like this: "String1|String2|String3|String4" from a device that serves ascii text via http and then make them variables so I can add them to a MySQL database (VB.net does talk with MySQL, right?) which will then be accessed by my PHP website I built. I already have the string of text in a string. The reason I want to do it with VB.net is because this would have to be run on a schedule every 5 minutes or so via Windows Scheduled Tasks or maybe even as a windows service.

So basically, I want to take "String1|String2|String3|String4" and make it Dim string1 As String, Dim string2 As String, Dim string3 As String, Dim string4 As String and then save it to the database via a query I haven't figured out yet :) If you have any knowledge on that, I would appreciate that too!

If this helps, the PHP equivalent to what I want to do would be something like explode($string, "|") list($string1, $string2, $string3, $string4)

Thanks!
Steve
 
you want to put the split values into an array of string varibles.


Dim myString() as String = strBigLongString.Split('|')

then access the items

myString(0), myString(1), etc.

 
tperri,
You need double quotes... This is not C language to treat the pipe as a char.
 
tperri, thanks for your reply. Is it possible to assign the array values myString(0), myString(1) to variable names a little easier to remember?

Thanks!
Steve
 
Yeah what he said..
Dumping vb for php??? Is you nuts??? [upsidedown]
Welcome back brutha!

If more than 1 goose are geese, why aren't more than 1 moose meese??
[censored][censored][censored]
 
be aware that rick's code doesn't give you a new handle to the array variables, it copies the data into a new variable.

you can't assign a new value to ThisIS... and have it reflected in the array.

if it's not really an array but separate fields held as an array, you should look into creating a struct or a class that takes the string as its constructor, splits it, and sets the value of member variables base on the resultant array.

Code:
Public Class Class1
    Private array As String()
    Public Sub New(ByVal aString As String)
        array = Split(aString, "|")
    End Sub
    Public Property Field1() As String
        Get
            Return array(0)
        End Get
        Set(ByVal value As String)
            array(0) = value
        End Set
    End Property
End Class

' elsewhere

Dim array as string() = aString.Split("|")
Dim myClass = new myClass(array)
...
' then instead of array(0) you can use:
doSomethingWith myClass.Field1



mr s. <;)

 
Yeah, sorry for the single quotes, I changed my code from C# to VB and forgot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top