Is there a repeat characters function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John L. Whelan

    #1

    Is there a repeat characters function?

    Hi Everyone:

    I know that I can repeat a single character using the StrDup function. Is
    there a function that will allow me to repeat a string? StrDup(5,"John" )
    returns "JJJJJ." Is there a function (or workaround) that will
    return"JohnJohn JohnJohnJohn"?

    Thanks in advance.

    John

    --
    John L. Whelan
    College Librarian
    College of the North Atlantic
    Grand Falls-Windsor, NL
  • Cor Ligthert [MVP]

    #2
    Re: Is there a repeat characters function?

    John,

    Even if it did exist I would do it in this way

    dim John as String
    For i as integer = 0 to 4
    John += "John"
    Next

    (And if it exist you can be sure that it is done internal the same)

    I would not take the time to search

    Cor

    "John L. Whelan" <John.Whelan@cn a.nl.caschreef in bericht
    news:81E90071-1AC1-4A61-B67F-C4DB1D46F83C@mi crosoft.com...
    Hi Everyone:
    >
    I know that I can repeat a single character using the StrDup function. Is
    there a function that will allow me to repeat a string? StrDup(5,"John" )
    returns "JJJJJ." Is there a function (or workaround) that will
    return"JohnJohn JohnJohnJohn"?
    >
    Thanks in advance.
    >
    John
    >
    --
    John L. Whelan
    College Librarian
    College of the North Atlantic
    Grand Falls-Windsor, NL

    Comment

    • sweet_dreams

      #3
      Re: Is there a repeat characters function?


      John L. Whelan napisal(a):
      Hi Everyone:
      >
      I know that I can repeat a single character using the StrDup function. Is
      there a function that will allow me to repeat a string? StrDup(5,"John" )
      returns "JJJJJ." Is there a function (or workaround) that will
      return"JohnJohn JohnJohnJohn"?
      >
      Thanks in advance.
      >
      John
      Hi,

      I don't know such a function but I wrote for you one:

      Private Function Repeat(ByVal StringToRepeat As String, ByVal
      HowManyTimes As Integer)
      Dim Result As String = ""
      For i As Integer = 1 To HowManyTimes
      Result += StringToRepeat
      Next
      Return Result
      End Function

      Hope this helps,
      sweet_dreams

      Comment

      • Claes Bergefall

        #4
        Re: Is there a repeat characters function?

        Don't think there is a built in way, but it's not so hard to build a
        function that does it. Try this:

        Public Function StrDup(ByVal count As Integer, ByVal value As String) As
        String
        Dim sb As New System.Text.Str ingBuilder
        While count 0
        sb.Append(value )
        count -= 1
        End While
        Return sb.ToString()
        End Function

        /claes

        "John L. Whelan" <John.Whelan@cn a.nl.cawrote in message
        news:81E90071-1AC1-4A61-B67F-C4DB1D46F83C@mi crosoft.com...
        Hi Everyone:
        >
        I know that I can repeat a single character using the StrDup function. Is
        there a function that will allow me to repeat a string? StrDup(5,"John" )
        returns "JJJJJ." Is there a function (or workaround) that will
        return"JohnJohn JohnJohnJohn"?
        >
        Thanks in advance.
        >
        John
        >
        --
        John L. Whelan
        College Librarian
        College of the North Atlantic
        Grand Falls-Windsor, NL

        Comment

        • Andrew Morton

          #5
          Re: Is there a repeat characters function?

          John L. Whelan wrote:
          Hi Everyone:
          >
          I know that I can repeat a single character using the StrDup
          function. Is there a function that will allow me to repeat a string?
          StrDup(5,"John" ) returns "JJJJJ." Is there a function (or workaround)
          that will return"JohnJohn JohnJohnJohn"?
          I can do this!

          Dim s As String
          s = StrDup(5, "x")
          s = s.Replace("x", "John")

          Or for a longer string:-

          Dim b As New StringBuilder
          b.Append("x", 500)
          b.Replace("x", "John")

          Andrew


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Is there a repeat characters function?

            Claes (and Andrew),
            I would recommend setting the capacity of the StringBuilder when you create
            it, this will prevent any intermediate reallocations of its internal buffer.
            Especially in this case where you have a clear indication of how large the
            final string will be.

            | Dim sb As New System.Text.Str ingBuilder(coun t * value.Length)

            The "count * value.Length" causes the StringBuilder to allocate a "count *
            value.Length" character buffer.

            Remember that New StringBuilder() allocates a 16 character buffer, which it
            then doubles each time it (the buffer) needs to be expanded.

            If you want "John" duplicated 500 times, StringBuilder will need to
            reallocate its buffer a number of a significant # of times. These
            reallocations can cause a performance problem in that the buffer itself
            needs to be copied each time it is reallocated, plus each old buffer will
            add pressure to the GC...

            In cases where I don't have a clear indication of how large the final string
            will be I try to use a guesstimate. For example the average/typical length
            of the final string.


            --
            Hope this helps
            Jay B. Harlow [MVP - Outlook]
            ..NET Application Architect, Enthusiast, & Evangelist
            T.S. Bradley - http://www.tsbradley.net


            "Claes Bergefall" <louplou@nospam .nospamwrote in message
            news:uX1Jy4gwGH A.4972@TK2MSFTN GP05.phx.gbl...
            | Don't think there is a built in way, but it's not so hard to build a
            | function that does it. Try this:
            |
            | Public Function StrDup(ByVal count As Integer, ByVal value As String) As
            | String
            | Dim sb As New System.Text.Str ingBuilder
            | While count 0
            | sb.Append(value )
            | count -= 1
            | End While
            | Return sb.ToString()
            | End Function
            |
            | /claes
            |
            | "John L. Whelan" <John.Whelan@cn a.nl.cawrote in message
            | news:81E90071-1AC1-4A61-B67F-C4DB1D46F83C@mi crosoft.com...
            | Hi Everyone:
            | >
            | I know that I can repeat a single character using the StrDup function.
            Is
            | there a function that will allow me to repeat a string? StrDup(5,"John" )
            | returns "JJJJJ." Is there a function (or workaround) that will
            | return"JohnJohn JohnJohnJohn"?
            | >
            | Thanks in advance.
            | >
            | John
            | >
            | --
            | John L. Whelan
            | College Librarian
            | College of the North Atlantic
            | Grand Falls-Windsor, NL
            |
            |


            Comment

            Working...