Link to home
Start Free TrialLog in
Avatar of Shiseiryu1
Shiseiryu1Flag for United States of America

asked on

.NET CF - Remove "�" characters from string

Hello,
  I have some code that grabs the owner information from the Pocket PC and puts it in a string.  Anyways, I want to break the string up into the specific pieces (first name, last name, city) but the pieces are dividied by a wierd character combination.  See below:

John�Smith�PO Box xxxx Some City, NA 55555�555-555-5555�test@test.com

If I try to do the following, it doesn't see the characters:  fname = left(thestring,"�")

But, If I try to do the following, the ampersand is underlined w/ a "expression expected" error: fname = left(thestring,�)


Any suggestions how I can split it up?  Also, what is that character combination anyways?  

Thanks.
Avatar of Mikal613
Mikal613
Flag of United States of America image

txtName.Text = ls
txtCompany.Text = Mid(ls, 37)
txtAddress.Text = Mid(ls, 73)
txtTelephone.Text = Mid(ls, 259)
txtEmail.Text = Mid(ls, 284)


 Registry.GetStringValue("\ControlPanel\Owner", "Owner", NewValue)
        MsgBox(NewValue)
MsgBox(Mid(NewValue, 37)) ' add this under it , its the Company
MsgBox(Mid(NewValue, 73)) ' add this under it , its the Address
MsgBox(Mid(NewValue, 259)) ' add this under it ,PhoneNumber
MsgBox(Mid(NewValue, 284)) ' add this under it , its the email address
ASKER CERTIFIED SOLUTION
Avatar of Mikal613
Mikal613
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shiseiryu1

ASKER

How do I seperate the first name from last name?

I tried:
firstname = left(NewValue,instr(NewValue," "))   'This works ok

lastname = right(NewValue,len(NewValue)-len(firstname))  'This grabs the last name but also a bunch of Null characters (�)


You can't notice the "�" characters until you try to write the string to XML.   Any ideas?



        Dim MySplitLine As Collection = SplitDelimitedLine(NewValue, "�" )
        MsgBox( MySplitLine.Item(1))

        MsgBox( MySplitLine.Item(2))

        MsgBox( MySplitLine.Item(3))

        MsgBox( MySplitLine.Item(4))

        MsgBox( MySplitLine.Item(5))
why dont you save the information in a variable and then send it to xml
I did.  I put the information in a string variables, then populated a dataset w/ them.  Then I wrote them to XML.  For some reason the string variables looked ok but they were still holding the "�" characters. Below shows what I was doing.  VB is complaining that "SplitDelmitedLine is not declared".  What do I need to import to make it work?  Are there a "standard" set of imports that you suggest using on every project?  Thanks again.


          'Initialize First Name
            theFirstName = Microsoft.VisualBasic.Left(theOwnerInfo, Microsoft.VisualBasic.InStr(theOwnerInfo, " "))
            theFirstName = Trim(theFirstName)

            'Initialize Last Name
            theLastName = Microsoft.VisualBasic.Mid(theOwnerInfo, 37)
            'theLastName = Microsoft.VisualBasic.Right(theOwnerInfo, Microsoft.VisualBasic.Len(theOwnerInfo) - Microsoft.VisualBasic.Len(theFirstName))

            'Initialize Email Address
            theEmail = Mid(theOwnerInfo, 284)
            theEmail = Trim(theEmail)

            'Same the Information to a Dataset
            MyDataSet.Tables(0).Rows(0).Item(0) = ""
            MyDataSet.Tables(0).Rows(0).Item(1) = theFirstName
            MyDataSet.Tables(0).Rows(0).Item(2) = theLastName
            MyDataSet.Tables(0).Rows(0).Item(3) = theEmail

            'Write the Dataset to the XML File
            MyDataSet.WriteXml(thePath)
Ok, I imported System.IO and put in the following function, but its asking for 3 parameters..not 2.  Suggestions?

    Function SplitDelimitedLine(ByVal CurrentLine As String, ByVal Delimiter As String, ByVal Qualifier As String) As Collection

        Dim i As Integer
        Dim SplitString As New Collection
        Dim CountDelimiter As Boolean
        Dim Total As Integer
        Dim Ch As Char
        Dim Section As String

        ' We want to count the delimiter unless it is within the text qualifier
        CountDelimiter = True
        Total = 0
        Section = ""

        For i = 1 To Len(CurrentLine)

            Ch = Mid(CurrentLine, i, 1)
            Select Case Ch

                Case Qualifier
                    If CountDelimiter Then
                        CountDelimiter = False
                    Else
                        CountDelimiter = True
                    End If

                Case Delimiter
                    If CountDelimiter Then

                        ' Add current section to collection
                        SplitString.Add(New MySection(Section))

                        Section = ""
                        Total = Total + 1

                    End If

                Case Else

                    Section = Section & Ch

            End Select


        Next

        ' Get the last field - as most files will not have an ending delimiter
        If CountDelimiter Then

            ' Add current section to collection
            SplitString.Add(New MySection(Section))

        End If


        SplitDelimitedLine = SplitString


    End Function

    Public Class MySection

        Public Section As String
        Sub New(ByVal newName As String)
            Section = newName
        End Sub

    End Class


Ok, I figured it out.  I had to create a function to check each character in the string and weed out the bad characters.

Function Cleanup(ByVal theString As String)

        'Declare Variables
        Dim goodChars As String
        Dim hasGood As Boolean
        Dim i As Integer
        Dim j As Integer

        'Initialize Good Characters
        goodChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'@."

        theString = Trim(theString)

        'Loop Through Each Character in the String
        For i = 0 To theString.Length - 1

            'Default to No Good Characters Found
            hasGood = False

            'Loop Through Each Good Character
            For j = 0 To goodChars.Length - 1

                'Check for Good Character
                If theString.Substring(i, 1) = goodChars.Substring(j, 1) Then
                    hasGood = True
                    Exit For
                End If

                'Check for Null
                If Microsoft.VisualBasic.IsNothing(theString.Substring(i, 1)) Then
                    hasGood = False
                    Exit For
                End If
            Next

            'No Good Character Found at Selected Index, Return Everything Previous
            If hasGood = False Then
                theString = Microsoft.VisualBasic.Left(theString, i)
                theString = Trim(theString)
                'BAD STUFF FOUND
                Return theString
            End If

        Next

        'EVERYTHING OK
        theString = Trim(theString)
        Return theString

    End Function
good for you

have a great weekend
btw:  Thanks for all your help Mikal613.  I'd still be stuck without your help.  :)  You have a nice weekend too.