Results 1 to 12 of 12

Thread: [Resolved][2005] String Manipulation

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Location
    Wisconsin, USA
    Posts
    29

    [Resolved][2005] String Manipulation

    I'm currently trying to get a string to be manipulated into this format

    Phone: (123) 456-7890
    Cell: (930) 345-4566
    Fax: (234) 454-4321

    From something a string that would say this
    1234567890
    2343454566
    2344544321

    This is my current code
    Code:
    Return "Phone: " & landLine & ControlChars.CrLf & "Cell: " & cell & ControlChars.CrLf & "Fax: " & fax
    Its Currently returning
    Phone: 1234567890
    Cell: 2343454566
    Fax: 2344544321

    Thanks for any help
    Last edited by BuRdeN; Apr 23rd, 2008 at 12:54 PM.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] String Manipulation

    If you want "()" and "-" in the String you are returning you, of course, will have to add them in there.

    EDIT: Like below.
    Last edited by nmadd; Apr 22nd, 2008 at 03:37 PM.

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2005] String Manipulation

    Code:
    Return String.Format("Phone: {0:(000) 000-0000}{3}Cell: {1:(000) 000-0000}{3}Fax: {2:(000) 000-0000}", landline, cell, fax, ControlChars.CrLf)
    This puts "landline" in {0}'s spot, and formats it (000) 000-000
    Then it puts "cell" in {1}'s spot and "fax" in {2}'s and does the same.
    Finally, it puts a CrLf in each {3} spot.

    If landline, cell and fax aren't numerical variables, you'll have problems.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Location
    Wisconsin, USA
    Posts
    29

    Re: [2005] String Manipulation

    Quote Originally Posted by Jenner
    If landline, cell and fax aren't numerical variables, you'll have problems.
    Hey Jenner thanks for helping me.
    I used that code you posted and it still returns the same thing I had before.

    How do I check and see if cell, fax and landline are numerical variables?

    Thanks, BuRdeN

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2005] String Manipulation

    Are they:

    Dim landline as Integer (or Long or Decimal or Double... anything but "String")?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Location
    Wisconsin, USA
    Posts
    29

    Re: [2005] String Manipulation

    They are in a Class Called. Phone.Vb

    Code:
    Option Explicit On
    Option Strict On
    
    Public Class Phone
        ' instance variables
        Private _landLine As String
        Private _fax As String
        Private _cell As String
    
        ' Constructor
        Public Sub New(Optional ByVal theLandLine As String = "", _
                       Optional ByVal theFax As String = "", _
                       Optional ByVal theCell As String = "")
            _landLine = theLandLine
            _fax = theFax
            _cell = theCell
        End Sub
        'Property Procedures
        Public Property landLine() As String
            Get
                Return _landLine
            End Get
            Set(ByVal value As String)
                _landLine = value
            End Set
        End Property
        Public Property fax() As String
            Get
                Return _fax
            End Get
            Set(ByVal value As String)
                _fax = value
            End Set
        End Property
        Public Property cell() As String
            Get
                Return _cell
            End Get
            Set(ByVal value As String)
                _cell = value
            End Set
        End Property
        'ToString Function
        Public Overrides Function ToString() As String
            Return String.Format("Phone: {0:(000) 000-0000}{3}Cell: {1:(000) 000-0000}{3}Fax: {2:(000) 000-0000}", landLine, cell, fax, ControlChars.CrLf)
        End Function
    End Class
    and this is the MainForm

    Code:
    Option Explicit On
    Option Strict On
    
    Public Class MainForm
    
        Private aPerson As Person
        Private anAddress As Address
        Private aPhone As Phone
    
    
        Private Sub MainForm_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
            ' instantiate objects
            aPerson = New Person
            anAddress = New Address
            aPhone = New Phone
        End Sub
    
        Private Sub displayButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles displayButton.Click
            ' get data from form, load into objects
            anAddress.Street = streetTextBox.Text
            anAddress.City = cityTextBox.Text
            anAddress.State = stateTextBox.Text
            anAddress.Zip = zipTextBox.Text
    
            aPerson.Name = nameTextBox.Text
            aPerson.Address = anAddress
    
            aPhone.cell = cellTextBox.Text
            aPhone.fax = faxTextBox.Text
            aPhone.landLine = landLineTextBox.Text
    
            ' display mailing address
            mailingLabel.Text = aPerson.ToString()
            'display phone information
            phoneLabel.Text = aPhone.ToString()
        End Sub
    
        Private Sub exitButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles exitButton.Click
            Me.Close()
        End Sub
    
    End Class

  7. #7
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2005] String Manipulation

    Well theres your problem. Your numbers are defined as string. You need to convert them to a integer. ( i am not sure what the maximum size for an integer is so)

    Use convert.tointeger(textboxvalue)

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Location
    Wisconsin, USA
    Posts
    29

    Re: [2005] String Manipulation

    Where would I have to convert them to integers?

  9. #9
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2005] String Manipulation

    Marked in red.

    vb.net Code:
    1. Public Class Phone
    2.     ' instance variables
    3.     Private _landLine As Double
    4.     Private _fax As Double
    5.     Private _cell As Double
    6.  
    7.     ' Constructor
    8.     Public Sub New(Optional ByVal theLandLine As Double = 0, _
    9.                    Optional ByVal theFax As Double = 0, _
    10.                    Optional ByVal theCell As Double = 0)
    11.         _landLine = theLandLine
    12.         _fax = theFax
    13.         _cell = theCell
    14.     End Sub
    15.  
    16.     'Property Procedures
    17.     Public Property landLine() As Double
    18.         Get
    19.             Return _landLine
    20.         End Get
    21.         Set(ByVal value As Double)
    22.             _landLine = value
    23.         End Set
    24.     End Property
    25.  
    26.     Public Property fax() As Double
    27.         Get
    28.             Return _fax
    29.         End Get
    30.         Set(ByVal value As Double)
    31.             _fax = value
    32.         End Set
    33.     End Property
    34.  
    35.     Public Property cell() As Double
    36.         Get
    37.             Return _cell
    38.         End Get
    39.         Set(ByVal value As Double)
    40.             _cell = value
    41.         End Set
    42.     End Property
    43.  
    44.     'ToString Function
    45.     Public Overrides Function ToString() As String
    46.         Return String.Format( _
    47.             "Phone: {0:(000) 000-0000}{3}Cell: {1:(000) 000-0000}{3}Fax: {2:(000) 000-0000}", _
    48.             landLine, cell, fax, ControlChars.CrLf _
    49.         )
    50.     End Function
    51. End Class

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] String Manipulation

    I wouldn't declare phone numbers as numeric data types. They are strings and should be treated as strings. The OP should add another property to his class to return the formatted phone number instead of changing the variable types.

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] String Manipulation

    I agree with stanav, strings.

    Code:
            Dim landLine As String = "(202) 456-1414"
            Dim landLineF As String
            'desired format
            '(NPA)spaceNXX-station
            'check
            landLine = landLine.Replace("-", "")
            landLine = landLine.Replace("(", "")
            landLine = landLine.Replace(")", "")
            landLine = landLine.Replace(" ", "")
            If landLine.Length < 10 Then landLine.PadLeft(10, "0"c)
            If landLine.Length > 10 Then landLine = landLine.Substring(0, 10)
            Debug.WriteLine(landLine)
            'now format it
            landLineF = "(" & landLine.Substring(0, 3) & ") " & landLine.Substring(3, 3) & "-" & landLine.Substring(6)
            Debug.WriteLine(landLineF)
            'or this as long as landLine is in that format
            landLineF = String.Format("{0 :(000) 000-0000}", Integer.Parse(landLine))
            Debug.WriteLine(landLineF)
    Last edited by dbasnett; Apr 23rd, 2008 at 07:35 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Location
    Wisconsin, USA
    Posts
    29

    Re: [2005] String Manipulation

    Quote Originally Posted by Deepak Sakpal
    Marked in red.

    vb.net Code:
    1. Public Class Phone
    2.     ' instance variables
    3.     Private _landLine As Double
    4.     Private _fax As Double
    5.     Private _cell As Double
    6.  
    7.     ' Constructor
    8.     Public Sub New(Optional ByVal theLandLine As Double = 0, _
    9.                    Optional ByVal theFax As Double = 0, _
    10.                    Optional ByVal theCell As Double = 0)
    11.         _landLine = theLandLine
    12.         _fax = theFax
    13.         _cell = theCell
    14.     End Sub
    15.  
    16.     'Property Procedures
    17.     Public Property landLine() As Double
    18.         Get
    19.             Return _landLine
    20.         End Get
    21.         Set(ByVal value As Double)
    22.             _landLine = value
    23.         End Set
    24.     End Property
    25.  
    26.     Public Property fax() As Double
    27.         Get
    28.             Return _fax
    29.         End Get
    30.         Set(ByVal value As Double)
    31.             _fax = value
    32.         End Set
    33.     End Property
    34.  
    35.     Public Property cell() As Double
    36.         Get
    37.             Return _cell
    38.         End Get
    39.         Set(ByVal value As Double)
    40.             _cell = value
    41.         End Set
    42.     End Property
    43.  
    44.     'ToString Function
    45.     Public Overrides Function ToString() As String
    46.         Return String.Format( _
    47.             "Phone: {0:(000) 000-0000}{3}Cell: {1:(000) 000-0000}{3}Fax: {2:(000) 000-0000}", _
    48.             landLine, cell, fax, ControlChars.CrLf _
    49.         )
    50.     End Function
    51. End Class
    This worked great! Thanks for your help DeePak + 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width