|
-
Apr 22nd, 2008, 03:28 PM
#1
Thread Starter
Junior Member
[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.
-
Apr 22nd, 2008, 03:33 PM
#2
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.
-
Apr 22nd, 2008, 03:36 PM
#3
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.
-
Apr 22nd, 2008, 03:45 PM
#4
Thread Starter
Junior Member
Re: [2005] String Manipulation
 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
-
Apr 22nd, 2008, 03:47 PM
#5
Re: [2005] String Manipulation
Are they:
Dim landline as Integer (or Long or Decimal or Double... anything but "String")?
-
Apr 22nd, 2008, 03:50 PM
#6
Thread Starter
Junior Member
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
-
Apr 22nd, 2008, 04:15 PM
#7
Fanatic Member
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)
-
Apr 22nd, 2008, 09:49 PM
#8
Thread Starter
Junior Member
Re: [2005] String Manipulation
Where would I have to convert them to integers?
-
Apr 23rd, 2008, 12:33 AM
#9
Re: [2005] String Manipulation
Marked in red.
vb.net Code:
Public Class Phone ' instance variables Private _landLine As Double Private _fax As Double Private _cell As Double ' Constructor Public Sub New(Optional ByVal theLandLine As Double = 0, _ Optional ByVal theFax As Double = 0, _ Optional ByVal theCell As Double = 0) _landLine = theLandLine _fax = theFax _cell = theCell End Sub 'Property Procedures Public Property landLine() As Double Get Return _landLine End Get Set(ByVal value As Double) _landLine = value End Set End Property Public Property fax() As Double Get Return _fax End Get Set(ByVal value As Double) _fax = value End Set End Property Public Property cell() As Double Get Return _cell End Get Set(ByVal value As Double) _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
Last edited by Deepak Sakpal; Apr 23rd, 2008 at 12:37 AM.
-
Apr 23rd, 2008, 05:25 AM
#10
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.
-
Apr 23rd, 2008, 07:28 AM
#11
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.
-
Apr 23rd, 2008, 12:53 PM
#12
Thread Starter
Junior Member
Re: [2005] String Manipulation
 Originally Posted by Deepak Sakpal
Marked in red.
vb.net Code:
Public Class Phone
' instance variables
Private _landLine As Double
Private _fax As Double
Private _cell As Double
' Constructor
Public Sub New(Optional ByVal theLandLine As Double = 0, _
Optional ByVal theFax As Double = 0, _
Optional ByVal theCell As Double = 0)
_landLine = theLandLine
_fax = theFax
_cell = theCell
End Sub
'Property Procedures
Public Property landLine() As Double
Get
Return _landLine
End Get
Set(ByVal value As Double)
_landLine = value
End Set
End Property
Public Property fax() As Double
Get
Return _fax
End Get
Set(ByVal value As Double)
_fax = value
End Set
End Property
Public Property cell() As Double
Get
Return _cell
End Get
Set(ByVal value As Double)
_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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|