[AccessD] Treat string as array

A.D.Tejpal adtp at airtelmail.in
Tue Mar 25 02:06:57 CDT 2008


    A very concise version of Function Fn_ConvStringToArray(), using only a single statement, is placed below. It returns an array, so that individual characters of input string get placed in individual elements of the array.

    Note - In fact, right side of assignment statement (A) below, can be used directly, as it is made up purely of access built-in functions. Fn_ConvStringToArray() acts merely as a wrapper function, for sake of convenience.

A.D.Tejpal
------------

' Code to be placed in VBA module
'================================
Function Fn_ConvStringToArray(StrInput _
                                As String) As Variant
    Fn_ConvStringToArray = _
                Split(Format(StrInput, _
                Mid(Replace(String(Len(StrInput), _
                "~"), "~", "~@"), 2)), "~")                         ' (A)
End Function
'================================

  ----- Original Message ----- 
  From: A.D.Tejpal 
  To: Access Developers discussion and problem solving 
  Cc: A.D.Tejpal 
  Sent: Tuesday, March 25, 2008 11:01
  Subject: Re: [AccessD] Treat string as array


      Function Fn_ConvStringToArray(), as given below, returns an array, so that individual characters of input string get placed in individual elements of the array.

      Note - The three line block of code involving For/Next loop could have been replaced by a single statement using String() function. However, String() function recognizes only one character as its last argument.

  A.D.Tejpal
  ------------

  ' Code to be placed in VBA module
  '=============================
  Function Fn_ConvStringToArray(StrInput _
                                  As String) As Variant
      Dim Fmt As String, Cnt As Long
      
      For Cnt = 1 To Len(StrInput)
          Fmt = Fmt & "~@"
      Next
      ' Strip leading ~
      Fmt = Mid(Fmt, 2)
      Fn_ConvStringToArray = _
                  Split(Format(StrInput, Fmt), "~")
  End Function
  '=============================

    ----- Original Message ----- 
    From: jwcolby 
    To: 'Access Developers discussion and problem solving' 
    Sent: Monday, March 24, 2008 22:19
    Subject: Re: [AccessD] Treat string as array


    Michael,

    Split() only works on delimited strings.  It is designed to break a string
    of words (for example) into an array of the words.  In that case it would
    use the spaces as the delimiter.

    I am trying to take any string and put the INDIVIDUAL CHARACTERS OF THE
    STRING into INDIVIDUAL ELEMENTS of an array. If I gave you a string of
    "12345asdfg" and you pass that to split you would get (I assume)
    "12345asdfg" back in one element of an array.  I need 1 in the first
    element, 2 in the next element, 3 in the next element etc.

    Go back to the very beginning of this thread and look at what I asked for.

    >> How can I turn a string into an array?  I need to iterate through the 
    >> characters of a string.
    >>
    >> For each char in str
    >> dosomething char
    >> Next char

    Arrays have iterators (for each next).  Strings do not.

    John W. Colby
    Colby Consulting
    www.ColbyConsulting.com 
    -----Original Message-----
    From: accessd-bounces at databaseadvisors.com
    [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Michael Bahr
    Sent: Monday, March 24, 2008 12:37 PM
    To: Access Developers discussion and problem solving
    Subject: Re: [AccessD] Treat string as array

    Ok, but the principles still apply.  Try this
    http://www.vbforums.com/showthread.php?t=470268

    and for reference
    http://www.codeguru.com/forum/archive/index.php/t-250108.html
    http://www.thescripts.com/forum/thread13305.html

    Mike...

    > Thanks.  VBA.
    >
    >
    > John W. Colby
    > Colby Consulting
    > www.ColbyConsulting.com
    > -----Original Message-----
    > From: accessd-bounces at databaseadvisors.com
    > [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Michael 
    > Bahr
    > Sent: Sunday, March 23, 2008 8:21 PM
    > To: Access Developers discussion and problem solving
    > Subject: Re: [AccessD] Treat string as array
    >
    > JOhn, take a look here
    > http://www.thescarms.com/dotnet/dotnetstring.aspx
    >
    > Mike...
    >
    >> How can I turn a string into an array?  I need to iterate through the 
    >> characters of a string.
    >>
    >> For each char in str
    >> dosomething char
    >> Next char
    >>
    >> John W. Colby
    >> Colby Consulting
    >> www.ColbyConsulting.com


More information about the AccessD mailing list