Results 1 to 5 of 5

Thread: Format string in VB.NET ??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99

    Unhappy Format string in VB.NET ??

    Hello everybody,


    in VB6, I did the following :

    myStr = format("hello","@@-@@-@") to receive this : he-ll-o

    I really can't seem to find it in VB.NET

    can anyone help me out please ??

    Thanks a lot !

    Bjorn

  2. #2
    Member
    Join Date
    Dec 2000
    Location
    UK
    Posts
    39
    Try String.Format

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99
    I've been trying it out but can't find a solution within the help

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Format seem to mainly format things that aren't text into some form of text. There isn't much to change the look of things that already are text it seems. BUT you can make your own Formatter to handle it. I'll try and find or make an example real quick.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. 'in the application
    2.         Dim myStr As String = "hello"
    3.         myStr = String.Format(New AlphaFormat("@@-@@-@"), "{0} is how it looks in the new format", myStr)
    4.         'can also do it like this:
    5.         'myStr = String.Format(New AlphaFormat(), "{0:@@-@@-@} is how it looks in the new format", myStr)
    6.         MsgBox(myStr)
    7.  
    8. 'the format class
    9. Public Class AlphaFormat
    10.     Implements IFormatProvider
    11.     Implements ICustomFormatter
    12.  
    13.     Private _format As String
    14.  
    15.     ' String.Format calls this method to get an instance of an ICustomFormatter to handle the formatting.
    16.     Public Function GetFormat(ByVal service As Type) As Object Implements IFormatProvider.GetFormat
    17.  
    18.         If service.ToString() = GetType(ICustomFormatter).ToString() Then
    19.             Return Me
    20.         Else
    21.             Return Nothing
    22.         End If
    23.     End Function
    24.  
    25.     ' After String.Format gets the ICustomFormatter, it calls this format method on each argument.
    26.     Public Function Format(ByVal theformat As String, ByVal arg As Object, ByVal provider As IFormatProvider) As String _
    27.     Implements ICustomFormatter.Format
    28.  
    29.         If theformat Is Nothing Then
    30.             'use _format
    31.             theformat = _format
    32.         End If
    33.  
    34.         If theformat Is Nothing Then
    35.             Return String.Format("{0}", arg)
    36.         End If
    37.  
    38.         If Not theformat.StartsWith("@") Then
    39.             ' If the object to be formatted supports the IFormattable interface, then pass the format specifier to the objects ToString method for formatting.
    40.             If TypeOf arg Is IFormattable Then
    41.                 Return CType(arg, IFormattable).ToString(Format, provider)
    42.             End If
    43.             ' If the object does not support IFormattable, then call the objects ToString method with no additional formatting.
    44.         ElseIf (arg Is Nothing) Then
    45.             Return arg.ToString()
    46.         End If
    47.  
    48.         Dim output As String
    49.         Dim c As Char
    50.         Dim cnt As Integer = 1
    51.         For Each c In theformat
    52.             If c = "@" Then
    53.                 If arg.length >= cnt Then
    54.                     c = Mid(arg, cnt)
    55.                     cnt += 1
    56.                 End If
    57.             End If
    58.             If c <> "@" Then
    59.                 output &= c
    60.             End If
    61.         Next
    62.  
    63.         Return output
    64.     End Function
    65.  
    66.     Public Sub New(ByVal format As String)
    67.        mybase.New()
    68.         _format = format
    69.     End Sub
    70.  
    71.    Public Sub New()
    72.        mybase.New()
    73.     End Sub
    74.  
    75. End Class
    Last edited by Edneeis; Sep 27th, 2002 at 11:56 AM.

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